xref: /aoo41x/main/sc/source/core/data/stlsheet.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 //------------------------------------------------------------------------
34 #include "document.hxx"
35 #include "stlsheet.hxx"
36 #include "stlpool.hxx"
37 
38 #include "scitems.hxx"
39 #include <editeng/boxitem.hxx>
40 #include <editeng/frmdiritem.hxx>
41 #include <editeng/lrspitem.hxx>
42 #include <svx/pageitem.hxx>
43 #include <editeng/paperinf.hxx>
44 #include <editeng/pbinitem.hxx>
45 #include <editeng/sizeitem.hxx>
46 #include <editeng/ulspitem.hxx>
47 #include <sfx2/printer.hxx>
48 #include <svl/itempool.hxx>
49 #include <svl/itemset.hxx>
50 #include <svl/smplhint.hxx>
51 #include "attrib.hxx"
52 
53 
54 #include <vcl/svapp.hxx>	// GetSettings()
55 
56 #include "globstr.hrc"
57 #include "sc.hrc"
58 //------------------------------------------------------------------------
59 
60 TYPEINIT1(ScStyleSheet, SfxStyleSheet);
61 
62 #define TWO_CM		1134
63 #define HFDIST_CM	142
64 
65 //========================================================================
66 
67 ScStyleSheet::ScStyleSheet( const String&		rName,
68                             ScStyleSheetPool&   rPoolP,
69 							SfxStyleFamily		eFamily,
70                             sal_uInt16              nMaskP )
71 
72     :   SfxStyleSheet   ( rName, rPoolP, eFamily, nMaskP )
73     , eUsage( UNKNOWN )
74 {
75 }
76 
77 //------------------------------------------------------------------------
78 
79 ScStyleSheet::ScStyleSheet( const ScStyleSheet& rStyle )
80     : SfxStyleSheet	( rStyle )
81     , eUsage( UNKNOWN )
82 {
83 }
84 
85 //------------------------------------------------------------------------
86 
87 __EXPORT ScStyleSheet::~ScStyleSheet()
88 {
89 }
90 
91 //------------------------------------------------------------------------
92 
93 sal_Bool __EXPORT ScStyleSheet::HasFollowSupport() const
94 {
95 	return sal_False;
96 }
97 
98 //------------------------------------------------------------------------
99 
100 sal_Bool __EXPORT ScStyleSheet::HasParentSupport () const
101 {
102 	sal_Bool bHasParentSupport = sal_False;
103 
104 	switch ( GetFamily() )
105 	{
106 		case SFX_STYLE_FAMILY_PARA:	bHasParentSupport = sal_True;	break;
107 		case SFX_STYLE_FAMILY_PAGE: bHasParentSupport = sal_False;	break;
108         default:
109         {
110             // added to avoid warnings
111         }
112 	}
113 
114 	return bHasParentSupport;
115 }
116 
117 //------------------------------------------------------------------------
118 
119 sal_Bool __EXPORT ScStyleSheet::SetParent( const String& rParentName )
120 {
121 	sal_Bool bResult = sal_False;
122 	String aEffName = rParentName;
123 	SfxStyleSheetBase* pStyle = rPool.Find( aEffName, nFamily );
124 	if (!pStyle)
125 	{
126 		SfxStyleSheetIterator* pIter = rPool.CreateIterator( nFamily, SFXSTYLEBIT_ALL );
127 		pStyle = pIter->First();
128 		if (pStyle)
129 			aEffName = pStyle->GetName();
130 	}
131 
132 	if ( pStyle && aEffName != GetName() )
133 	{
134 		bResult = SfxStyleSheet::SetParent( aEffName );
135 		if (bResult)
136 		{
137 			SfxItemSet& rParentSet = pStyle->GetItemSet();
138 			GetItemSet().SetParent( &rParentSet );
139 
140             // #i113491# Drag&Drop in the stylist's hierarchical view doesn't execute a slot,
141             // so the repaint has to come from here (after modifying the ItemSet).
142             // RepaintRange checks the document's IsVisible flag and locked repaints.
143             ScDocument* pDoc = static_cast<ScStyleSheetPool&>(GetPool()).GetDocument();
144             if (pDoc)
145                 pDoc->RepaintRange( ScRange( 0,0,0, MAXCOL,MAXROW,MAXTAB ) );
146 		}
147 	}
148 
149 	return bResult;
150 }
151 
152 //------------------------------------------------------------------------
153 
154 SfxItemSet& __EXPORT ScStyleSheet::GetItemSet()
155 {
156 	if ( !pSet )
157 	{
158 		switch ( GetFamily() )
159 		{
160 			case SFX_STYLE_FAMILY_PAGE:
161 				{
162 					// Seitenvorlagen sollen nicht ableitbar sein,
163 					// deshalb werden an dieser Stelle geeignete
164 					// Werte eingestellt. (==Standard-Seitenvorlage)
165 
166                     SfxItemPool& rItemPool = GetPool().GetPool();
167                     pSet = new SfxItemSet( rItemPool,
168 										   ATTR_BACKGROUND, ATTR_BACKGROUND,
169 										   ATTR_BORDER, ATTR_SHADOW,
170                                            ATTR_LRSPACE, ATTR_PAGE_SCALETO,
171 										   ATTR_WRITINGDIR, ATTR_WRITINGDIR,
172 										   ATTR_USERDEF, ATTR_USERDEF,
173 										   0 );
174 
175 					//	Wenn gerade geladen wird, wird auch der Set hinterher aus der Datei
176 					//	gefuellt, es brauchen also keine Defaults gesetzt zu werden.
177 					//	GetPrinter wuerde dann auch einen neuen Printer anlegen, weil der
178 					//	gespeicherte Printer noch nicht geladen ist!
179 
180 					ScDocument* pDoc = ((ScStyleSheetPool&)GetPool()).GetDocument();
181 					if ( pDoc )
182 					{
183 						// Setzen von sinnvollen Default-Werten:
184 						SvxPageItem		aPageItem( ATTR_PAGE );
185 						SvxSizeItem		aPaperSizeItem( ATTR_PAGE_SIZE, SvxPaperInfo::GetDefaultPaperSize() );
186 
187 						SvxSetItem		aHFSetItem(
188 											(const SvxSetItem&)
189                                             rItemPool.GetDefaultItem(ATTR_PAGE_HEADERSET) );
190 
191 						SfxItemSet&		rHFSet = aHFSetItem.GetItemSet();
192 						SvxSizeItem		aHFSizeItem( // 0,5 cm + Abstand
193 											ATTR_PAGE_SIZE,
194 											Size( 0, (long)( 500 / HMM_PER_TWIPS ) + HFDIST_CM ) );
195 
196 						SvxULSpaceItem	aHFDistItem	( HFDIST_CM,// nUp
197 													  HFDIST_CM,// nLow
198 													  ATTR_ULSPACE );
199 
200 						SvxLRSpaceItem	aLRSpaceItem( TWO_CM,	// nLeft
201 													  TWO_CM,	// nRight
202 													  TWO_CM,	// nTLeft
203 													  0,		// nFirstLineOffset
204 													  ATTR_LRSPACE );
205 						SvxULSpaceItem	aULSpaceItem( TWO_CM,	// nUp
206 													  TWO_CM,	// nLow
207 													  ATTR_ULSPACE );
208 						SvxBoxInfoItem	aBoxInfoItem( ATTR_BORDER_INNER );
209 
210 						aBoxInfoItem.SetTable( sal_False );
211 						aBoxInfoItem.SetDist( sal_True );
212 						aBoxInfoItem.SetValid( VALID_DISTANCE, sal_True );
213 
214 						// aPageItem.SetLandscape( ORIENTATION_LANDSCAPE == pPrinter->GetOrientation() );
215 						aPageItem.SetLandscape( sal_False );
216 
217 						rHFSet.Put( aBoxInfoItem );
218 						rHFSet.Put( aHFSizeItem );
219 						rHFSet.Put( aHFDistItem );
220 						rHFSet.Put( SvxLRSpaceItem( 0,0,0,0, ATTR_LRSPACE ) ); // Rand auf Null setzen
221 
222 						pSet->Put( aHFSetItem, ATTR_PAGE_HEADERSET );
223 						pSet->Put( aHFSetItem, ATTR_PAGE_FOOTERSET );
224 						pSet->Put( aBoxInfoItem ); // PoolDefault wg. Formatvorlagen
225 												   // nicht ueberschreiben!
226 
227 						//	Writing direction: not as pool default because the default for cells
228 						//	must remain FRMDIR_ENVIRONMENT, and each page style's setting is
229 						//	supposed to be saved in the file format.
230 						//	The page default depends on the system language.
231 						SvxFrameDirection eDirection = ScGlobal::IsSystemRTL() ?
232 										FRMDIR_HORI_RIGHT_TOP : FRMDIR_HORI_LEFT_TOP;
233                         pSet->Put( SvxFrameDirectionItem( eDirection, ATTR_WRITINGDIR ), ATTR_WRITINGDIR );
234 
235                         rItemPool.SetPoolDefaultItem( aPageItem );
236                         rItemPool.SetPoolDefaultItem( aPaperSizeItem );
237                         rItemPool.SetPoolDefaultItem( aLRSpaceItem );
238                         rItemPool.SetPoolDefaultItem( aULSpaceItem );
239                         rItemPool.SetPoolDefaultItem( SfxUInt16Item( ATTR_PAGE_SCALE, 100 ) );
240                         ScPageScaleToItem aScaleToItem;
241                         rItemPool.SetPoolDefaultItem( aScaleToItem );
242                         rItemPool.SetPoolDefaultItem( SfxUInt16Item( ATTR_PAGE_SCALETOPAGES, 0 ) );
243 					}
244 				}
245 				break;
246 
247 			case SFX_STYLE_FAMILY_PARA:
248 			default:
249 				pSet = new SfxItemSet( GetPool().GetPool(),
250 									   ATTR_PATTERN_START, ATTR_PATTERN_END,
251 									   0 );
252 				break;
253 		}
254 		bMySet = sal_True;
255 	} // if ( !pSet )
256     if ( nHelpId == HID_SC_SHEET_CELL_ERG1 )
257     {
258         if ( !pSet->Count() )
259         {
260             ScDocument* pDoc = ((ScStyleSheetPool&)GetPool()).GetDocument();
261 	        if ( pDoc )
262 	        {
263                 sal_uLong nNumFmt = pDoc->GetFormatTable()->GetStandardFormat( NUMBERFORMAT_CURRENCY,ScGlobal::eLnge );
264 	            pSet->Put( SfxUInt32Item( ATTR_VALUE_FORMAT, nNumFmt ) );
265             } // if ( pDoc && pDoc->IsLoadingDone() )
266         }
267     }
268 
269 	return *pSet;
270 }
271 
272 //------------------------------------------------------------------------
273 
274 sal_Bool __EXPORT ScStyleSheet::IsUsed() const
275 {
276 	if ( GetFamily() == SFX_STYLE_FAMILY_PARA )
277 	{
278         // Always query the document to let it decide if a rescan is necessary,
279         // and store the state.
280         ScDocument* pDoc = ((ScStyleSheetPool&)rPool).GetDocument();
281         if ( pDoc && pDoc->IsStyleSheetUsed( *this, sal_True ) )
282             eUsage = USED;
283         else
284             eUsage = NOTUSED;
285         return eUsage == USED;
286 	}
287 	else
288 		return sal_True;
289 }
290 
291 //------------------------------------------------------------------------
292 
293 void __EXPORT ScStyleSheet::Notify( SfxBroadcaster&, const SfxHint& rHint )
294 {
295 	if ( rHint.ISA(SfxSimpleHint) )
296 		if ( ((SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
297 			GetItemSet().SetParent( NULL );
298 }
299 
300 //------------------------------------------------------------------------
301 
302 //	#66123# schmutzige Tricks, um die Standard-Vorlage immer als "Standard" zu speichern,
303 //	obwohl der fuer den Benutzer sichtbare Name uebersetzt ist:
304 
305 const String& ScStyleSheet::GetName() const
306 {
307 	const String& rBase = SfxStyleSheet::GetName();
308 	const String* pForceStdName = ((ScStyleSheetPool&)rPool).GetForceStdName();
309 	if ( pForceStdName && rBase == ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
310 		return *pForceStdName;
311 	else
312 		return rBase;
313 }
314 
315 const String& ScStyleSheet::GetParent() const
316 {
317 	const String& rBase = SfxStyleSheet::GetParent();
318 	const String* pForceStdName = ((ScStyleSheetPool&)rPool).GetForceStdName();
319 	if ( pForceStdName && rBase == ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
320 		return *pForceStdName;
321 	else
322 		return rBase;
323 }
324 
325 const String& ScStyleSheet::GetFollow() const
326 {
327 	const String& rBase = SfxStyleSheet::GetFollow();
328 	const String* pForceStdName = ((ScStyleSheetPool&)rPool).GetForceStdName();
329 	if ( pForceStdName && rBase == ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
330 		return *pForceStdName;
331 	else
332 		return rBase;
333 }
334 
335 //	Verhindern, dass ein Style "Standard" angelegt wird, wenn das nicht der
336 //	Standard-Name ist, weil sonst beim Speichern zwei Styles denselben Namen haetten
337 //	(Beim Laden wird der Style direkt per Make mit dem Namen erzeugt, so dass diese
338 //	Abfrage dann nicht gilt)
339 //!	Wenn irgendwann aus dem Laden SetName aufgerufen wird, muss fuer das Laden ein
340 //!	Flag gesetzt und abgefragt werden.
341 //!	Die ganze Abfrage muss raus, wenn fuer eine neue Datei-Version die Namens-Umsetzung wegfaellt.
342 
343 sal_Bool ScStyleSheet::SetName( const String& rNew )
344 {
345 	String aFileStdName = String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM(STRING_STANDARD));
346 	if ( rNew == aFileStdName && aFileStdName != ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
347 		return sal_False;
348 	else
349 		return SfxStyleSheet::SetName( rNew );
350 }
351 
352 
353 
354