Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "document.hxx"
21 : #include "stlsheet.hxx"
22 : #include "stlpool.hxx"
23 :
24 : #include "scitems.hxx"
25 : #include <editeng/boxitem.hxx>
26 : #include <editeng/frmdiritem.hxx>
27 : #include <editeng/lrspitem.hxx>
28 : #include <svx/pageitem.hxx>
29 : #include <editeng/paperinf.hxx>
30 : #include <editeng/pbinitem.hxx>
31 : #include <editeng/sizeitem.hxx>
32 : #include <editeng/ulspitem.hxx>
33 : #include <sfx2/printer.hxx>
34 : #include <svl/itempool.hxx>
35 : #include <svl/itemset.hxx>
36 : #include <svl/smplhint.hxx>
37 : #include "attrib.hxx"
38 :
39 : #include <vcl/svapp.hxx>
40 :
41 : #include "globstr.hrc"
42 : #include "sc.hrc"
43 :
44 0 : TYPEINIT1(ScStyleSheet, SfxStyleSheet);
45 :
46 : #define TWO_CM 1134
47 : #define HFDIST_CM 142
48 :
49 5644 : ScStyleSheet::ScStyleSheet( const OUString& rName,
50 : ScStyleSheetPool& rPoolP,
51 : SfxStyleFamily eFamily,
52 : sal_uInt16 nMaskP )
53 :
54 : : SfxStyleSheet ( rName, rPoolP, eFamily, nMaskP )
55 5644 : , eUsage( UNKNOWN )
56 : {
57 5644 : }
58 :
59 0 : ScStyleSheet::ScStyleSheet( const ScStyleSheet& rStyle )
60 : : SfxStyleSheet ( rStyle )
61 0 : , eUsage( UNKNOWN )
62 : {
63 0 : }
64 :
65 11134 : ScStyleSheet::~ScStyleSheet()
66 : {
67 11134 : }
68 :
69 0 : bool ScStyleSheet::HasFollowSupport() const
70 : {
71 0 : return false;
72 : }
73 :
74 16 : bool ScStyleSheet::HasParentSupport () const
75 : {
76 16 : bool bHasParentSupport = false;
77 :
78 16 : switch ( GetFamily() )
79 : {
80 12 : case SFX_STYLE_FAMILY_PARA: bHasParentSupport = true; break;
81 4 : case SFX_STYLE_FAMILY_PAGE: bHasParentSupport = false; break;
82 : default:
83 : {
84 : // added to avoid warnings
85 : }
86 : }
87 :
88 16 : return bHasParentSupport;
89 : }
90 :
91 7152 : bool ScStyleSheet::SetParent( const OUString& rParentName )
92 : {
93 7152 : bool bResult = false;
94 7152 : OUString aEffName = rParentName;
95 7152 : SfxStyleSheetBase* pStyle = pPool->Find( aEffName, nFamily );
96 7152 : if (!pStyle)
97 : {
98 1162 : SfxStyleSheetIteratorPtr pIter = pPool->CreateIterator( nFamily, SFXSTYLEBIT_ALL );
99 1162 : pStyle = pIter->First();
100 1162 : if (pStyle)
101 1144 : aEffName = pStyle->GetName();
102 : }
103 :
104 7152 : if ( pStyle && aEffName != GetName() )
105 : {
106 6946 : bResult = SfxStyleSheet::SetParent( aEffName );
107 6946 : if (bResult)
108 : {
109 6946 : SfxItemSet& rParentSet = pStyle->GetItemSet();
110 6946 : GetItemSet().SetParent( &rParentSet );
111 :
112 : // #i113491# Drag&Drop in the stylist's hierarchical view doesn't execute a slot,
113 : // so the repaint has to come from here (after modifying the ItemSet).
114 : // RepaintRange checks the document's IsVisible flag and locked repaints.
115 6946 : ScDocument* pDoc = static_cast<ScStyleSheetPool&>(GetPool()).GetDocument();
116 6946 : if (pDoc)
117 6946 : pDoc->RepaintRange( ScRange( 0,0,0, MAXCOL,MAXROW,MAXTAB ) );
118 : }
119 : }
120 :
121 7152 : return bResult;
122 : }
123 :
124 29 : void ScStyleSheet::ResetParent()
125 : {
126 29 : GetItemSet().SetParent(NULL);
127 29 : }
128 :
129 1152963 : SfxItemSet& ScStyleSheet::GetItemSet()
130 : {
131 1152963 : if ( !pSet )
132 : {
133 5644 : switch ( GetFamily() )
134 : {
135 : case SFX_STYLE_FAMILY_PAGE:
136 : {
137 : // Page templates should not be derivable,
138 : // therefore suitable values are set at this point.
139 : // (== Standard page template)
140 :
141 1816 : SfxItemPool& rItemPool = GetPool().GetPool();
142 : pSet = new SfxItemSet( rItemPool,
143 : ATTR_BACKGROUND, ATTR_BACKGROUND,
144 : ATTR_BORDER, ATTR_SHADOW,
145 : ATTR_LRSPACE, ATTR_PAGE_SCALETO,
146 : ATTR_WRITINGDIR, ATTR_WRITINGDIR,
147 : ATTR_USERDEF, ATTR_USERDEF,
148 1816 : 0 );
149 :
150 : // If being loaded also the set is then filled in from the file,
151 : // so the defaults do not need to be set.
152 : // GetPrinter would then also create a new printer,
153 : // because the stored Printer is not loaded yet!
154 :
155 1816 : ScDocument* pDoc = static_cast<ScStyleSheetPool&>(GetPool()).GetDocument();
156 1816 : if ( pDoc )
157 : {
158 : // Setting reasonable default values:
159 1816 : SvxPageItem aPageItem( ATTR_PAGE );
160 3632 : SvxSizeItem aPaperSizeItem( ATTR_PAGE_SIZE, SvxPaperInfo::GetDefaultPaperSize() );
161 :
162 : SvxSetItem aHFSetItem(
163 : static_cast<const SvxSetItem&>(
164 3632 : rItemPool.GetDefaultItem(ATTR_PAGE_HEADERSET) ));
165 :
166 1816 : SfxItemSet& rHFSet = aHFSetItem.GetItemSet();
167 : SvxSizeItem aHFSizeItem( // 0,5 cm + distance
168 : ATTR_PAGE_SIZE,
169 3632 : Size( 0, (long)( 500 / HMM_PER_TWIPS ) + HFDIST_CM ) );
170 :
171 : SvxULSpaceItem aHFDistItem ( HFDIST_CM,// nUp
172 : HFDIST_CM,// nLow
173 3632 : ATTR_ULSPACE );
174 :
175 : SvxLRSpaceItem aLRSpaceItem( TWO_CM, // nLeft
176 : TWO_CM, // nRight
177 : TWO_CM, // nTLeft
178 : 0, // nFirstLineOffset
179 3632 : ATTR_LRSPACE );
180 : SvxULSpaceItem aULSpaceItem( TWO_CM, // nUp
181 : TWO_CM, // nLow
182 3632 : ATTR_ULSPACE );
183 3632 : SvxBoxInfoItem aBoxInfoItem( ATTR_BORDER_INNER );
184 :
185 1816 : aBoxInfoItem.SetTable( false );
186 1816 : aBoxInfoItem.SetDist( true );
187 1816 : aBoxInfoItem.SetValid( SvxBoxInfoItemValidFlags::DISTANCE, true );
188 :
189 1816 : aPageItem.SetLandscape( false );
190 :
191 1816 : rHFSet.Put( aBoxInfoItem );
192 1816 : rHFSet.Put( aHFSizeItem );
193 1816 : rHFSet.Put( aHFDistItem );
194 1816 : rHFSet.Put( SvxLRSpaceItem( 0,0,0,0, ATTR_LRSPACE ) ); // Set border to Null
195 :
196 1816 : pSet->Put( aHFSetItem, ATTR_PAGE_HEADERSET );
197 1816 : pSet->Put( aHFSetItem, ATTR_PAGE_FOOTERSET );
198 1816 : pSet->Put( aBoxInfoItem ); // Do not overwrite PoolDefault
199 : // due to format templates
200 :
201 :
202 : // Writing direction: not as pool default because the default for cells
203 : // must remain FRMDIR_ENVIRONMENT, and each page style's setting is
204 : // supposed to be saved in the file format.
205 : // The page default depends on the system language.
206 1816 : SvxFrameDirection eDirection = ScGlobal::IsSystemRTL() ?
207 1816 : FRMDIR_HORI_RIGHT_TOP : FRMDIR_HORI_LEFT_TOP;
208 1816 : pSet->Put( SvxFrameDirectionItem( eDirection, ATTR_WRITINGDIR ), ATTR_WRITINGDIR );
209 :
210 1816 : rItemPool.SetPoolDefaultItem( aPageItem );
211 1816 : rItemPool.SetPoolDefaultItem( aPaperSizeItem );
212 1816 : rItemPool.SetPoolDefaultItem( aLRSpaceItem );
213 1816 : rItemPool.SetPoolDefaultItem( aULSpaceItem );
214 1816 : rItemPool.SetPoolDefaultItem( SfxUInt16Item( ATTR_PAGE_SCALE, 100 ) );
215 3632 : ScPageScaleToItem aScaleToItem;
216 1816 : rItemPool.SetPoolDefaultItem( aScaleToItem );
217 3632 : rItemPool.SetPoolDefaultItem( SfxUInt16Item( ATTR_PAGE_SCALETOPAGES, 0 ) );
218 : }
219 : }
220 1816 : break;
221 :
222 : case SFX_STYLE_FAMILY_PARA:
223 : default:
224 3828 : pSet = new SfxItemSet( GetPool().GetPool(), ATTR_PATTERN_START, ATTR_PATTERN_END );
225 3828 : break;
226 : }
227 5644 : bMySet = true;
228 : }
229 1152963 : if ( nHelpId == HID_SC_SHEET_CELL_ERG1 )
230 : {
231 9925 : if ( !pSet->Count() )
232 : {
233 : // Hack to work around that when this code is called from
234 : // ~ScStyleSheetPool -> ~SfxStyleSheetPool, GetPool() is no longer
235 : // an ScStyleSheetPool:
236 : ScStyleSheetPool * pool = dynamic_cast<ScStyleSheetPool *>(
237 772 : &GetPool());
238 772 : if (pool != nullptr) {
239 344 : ScDocument* pDoc = pool->GetDocument();
240 344 : if ( pDoc )
241 : {
242 344 : sal_uLong nNumFmt = pDoc->GetFormatTable()->GetStandardFormat( css::util::NumberFormat::CURRENCY,ScGlobal::eLnge );
243 344 : pSet->Put( SfxUInt32Item( ATTR_VALUE_FORMAT, nNumFmt ) );
244 : }
245 : }
246 : }
247 : }
248 :
249 1152963 : return *pSet;
250 : }
251 :
252 122 : bool ScStyleSheet::IsUsed() const
253 : {
254 122 : if ( GetFamily() == SFX_STYLE_FAMILY_PARA )
255 : {
256 : // Always query the document to let it decide if a rescan is necessary,
257 : // and store the state.
258 0 : ScDocument* pDoc = static_cast<ScStyleSheetPool*>(pPool)->GetDocument();
259 0 : if ( pDoc && pDoc->IsStyleSheetUsed( *this, true ) )
260 0 : eUsage = USED;
261 : else
262 0 : eUsage = NOTUSED;
263 0 : return eUsage == USED;
264 : }
265 : else
266 122 : return true;
267 : }
268 :
269 6314 : void ScStyleSheet::Notify( SfxBroadcaster&, const SfxHint& rHint )
270 : {
271 6314 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>(&rHint);
272 6314 : if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
273 3157 : GetItemSet().SetParent( NULL );
274 6314 : }
275 :
276 : // Dirty tricks, to always save the default template as "standard"
277 : // even though when shown to the user it is renamed:
278 :
279 1484594 : const OUString& ScStyleSheet::GetName() const
280 : {
281 1484594 : const OUString& rBase = SfxStyleSheet::GetName();
282 1484594 : const OUString* pForceStdName = static_cast<ScStyleSheetPool*>(pPool)->GetForceStdName();
283 1484594 : if ( pForceStdName && rBase == ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
284 0 : return *pForceStdName;
285 : else
286 1484594 : return rBase;
287 : }
288 :
289 7775 : const OUString& ScStyleSheet::GetParent() const
290 : {
291 7775 : const OUString& rBase = SfxStyleSheet::GetParent();
292 7775 : const OUString* pForceStdName = static_cast<ScStyleSheetPool*>(pPool)->GetForceStdName();
293 7775 : if ( pForceStdName && rBase == ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
294 0 : return *pForceStdName;
295 : else
296 7775 : return rBase;
297 : }
298 :
299 0 : const OUString& ScStyleSheet::GetFollow() const
300 : {
301 0 : const OUString& rBase = SfxStyleSheet::GetFollow();
302 0 : const OUString* pForceStdName = static_cast<ScStyleSheetPool*>(pPool)->GetForceStdName();
303 0 : if ( pForceStdName && rBase == ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
304 0 : return *pForceStdName;
305 : else
306 0 : return rBase;
307 : }
308 :
309 : // Avoid creating a Style "Standard" if this is not the Standard-Name;
310 : // otherwise two styles would have the same name when storing.
311 : // (on loading the style is created directly per Make with the name; making this query
312 : // not applicable)
313 : //TODO: If at any time during loading SetName is called, a flag has to be set/checked for loading
314 : //TODO: The whole check has to be removed if for a new file version the name transformation is dropped.
315 :
316 0 : bool ScStyleSheet::SetName(const OUString& rNew, bool bReindexNow)
317 : {
318 0 : OUString aFileStdName = OUString(STRING_STANDARD);
319 0 : if ( rNew == aFileStdName && aFileStdName != ScGlobal::GetRscString(STR_STYLENAME_STANDARD) )
320 0 : return false;
321 : else
322 0 : return SfxStyleSheet::SetName(rNew, bReindexNow);
323 156 : }
324 :
325 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|