LibreOffice Module sc (master)  1
stlsheet.cxx
Go to the documentation of this file.
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/shaditem.hxx>
31 #include <editeng/sizeitem.hxx>
32 #include <editeng/ulspitem.hxx>
33 #include <editeng/xmlcnitm.hxx>
34 #include <svl/itempool.hxx>
35 #include <svl/itemset.hxx>
36 #include <svl/numformat.hxx>
37 #include <svl/hint.hxx>
38 #include <o3tl/unit_conversion.hxx>
39 #include <attrib.hxx>
40 
41 #include <globstr.hrc>
42 #include <scresid.hxx>
43 #include <sc.hrc>
44 
47 
48 ScStyleSheet::ScStyleSheet( const OUString& rName,
49  const ScStyleSheetPool& rPoolP,
50  SfxStyleFamily eFamily,
51  SfxStyleSearchBits nMaskP )
52 
53  : SfxStyleSheet ( rName, rPoolP, eFamily, nMaskP )
54  , eUsage( Usage::UNKNOWN )
55 {
56 }
57 
59  : SfxStyleSheet ( rStyle )
60  , eUsage( Usage::UNKNOWN )
61 {
62 }
63 
65 {
66 }
67 
69 {
70  return false;
71 }
72 
74 {
75  bool bHasParentSupport = false;
76 
77  switch ( GetFamily() )
78  {
79  case SfxStyleFamily::Para: bHasParentSupport = true; break;
80  case SfxStyleFamily::Page: bHasParentSupport = false; break;
81  default:
82  {
83  // added to avoid warnings
84  }
85  }
86 
87  return bHasParentSupport;
88 }
89 
90 bool ScStyleSheet::SetParent( const OUString& rParentName )
91 {
92  bool bResult = false;
93  OUString aEffName = rParentName;
94  SfxStyleSheetBase* pStyle = m_pPool->Find( aEffName, nFamily );
95  if (!pStyle)
96  {
97  std::unique_ptr<SfxStyleSheetIterator> pIter = m_pPool->CreateIterator(nFamily);
98  pStyle = pIter->First();
99  if (pStyle)
100  aEffName = pStyle->GetName();
101  }
102 
103  if ( pStyle && aEffName != GetName() )
104  {
105  bResult = SfxStyleSheet::SetParent( aEffName );
106  if (bResult)
107  {
108  SfxItemSet& rParentSet = pStyle->GetItemSet();
109  GetItemSet().SetParent( &rParentSet );
110 
111  // #i113491# Drag&Drop in the stylist's hierarchical view doesn't execute a slot,
112  // so the repaint has to come from here (after modifying the ItemSet).
113  // RepaintRange checks the document's IsVisible flag and locked repaints.
114  ScDocument* pDoc = static_cast<ScStyleSheetPool*>(GetPool())->GetDocument();
115  if (pDoc)
116  pDoc->RepaintRange( ScRange( 0,0,0, MAXCOL,MAXROW,MAXTAB ) );
117  }
118  }
119 
120  return bResult;
121 }
122 
124 {
125  GetItemSet().SetParent(nullptr);
126 }
127 
129 {
130  if ( !pSet )
131  {
132  switch ( GetFamily() )
133  {
134  case SfxStyleFamily::Page:
135  {
136  // Page templates should not be derivable,
137  // therefore suitable values are set at this point.
138  // (== Standard page template)
139 
140  SfxItemPool& rItemPool = GetPool()->GetPool();
141  pSet = new SfxItemSet(
142  rItemPool,
143  svl::Items<
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  ScDocument* pDoc = static_cast<ScStyleSheetPool*>(GetPool())->GetDocument();
156  if ( pDoc )
157  {
158  // Setting reasonable default values:
159  SvxPageItem aPageItem( ATTR_PAGE );
161 
162  SvxSetItem aHFSetItem(
163  rItemPool.GetDefaultItem(ATTR_PAGE_HEADERSET) );
164 
165  SfxItemSet& rHFSet = aHFSetItem.GetItemSet();
166  SvxSizeItem aHFSizeItem( // 0,5 cm + distance
169 
170  SvxULSpaceItem aHFDistItem ( HFDIST_CM,// nUp
171  HFDIST_CM,// nLow
172  ATTR_ULSPACE );
173 
174  SvxLRSpaceItem aLRSpaceItem( TWO_CM, // nLeft
175  TWO_CM, // nRight
176  TWO_CM, // nTLeft
177  0, // nFirstLineOffset
178  ATTR_LRSPACE );
179  SvxULSpaceItem aULSpaceItem( TWO_CM, // nUp
180  TWO_CM, // nLow
181  ATTR_ULSPACE );
182  SvxBoxInfoItem aBoxInfoItem( ATTR_BORDER_INNER );
183 
184  aBoxInfoItem.SetTable( false );
185  aBoxInfoItem.SetDist( true );
186  aBoxInfoItem.SetValid( SvxBoxInfoItemValidFlags::DISTANCE );
187 
188  aPageItem.SetLandscape( false );
189 
190  rHFSet.Put( aBoxInfoItem );
191  rHFSet.Put( aHFSizeItem );
192  rHFSet.Put( aHFDistItem );
193  rHFSet.Put( SvxLRSpaceItem( 0,0,0,0, ATTR_LRSPACE ) ); // Set border to Null
194 
195  aHFSetItem.SetWhich(ATTR_PAGE_HEADERSET);
196  pSet->Put( aHFSetItem );
197  aHFSetItem.SetWhich(ATTR_PAGE_FOOTERSET);
198  pSet->Put( aHFSetItem );
199  pSet->Put( aBoxInfoItem ); // Do not overwrite PoolDefault
200  // due to format templates
201 
202 
203  // Writing direction: not as pool default because the default for cells
204  // must remain SvxFrameDirection::Environment, and each page style's setting is
205  // supposed to be saved in the file format.
206  // The page default depends on the system language.
208  SvxFrameDirection::Horizontal_RL_TB : SvxFrameDirection::Horizontal_LR_TB;
209  pSet->Put( SvxFrameDirectionItem( eDirection, ATTR_WRITINGDIR ) );
210 
211  rItemPool.SetPoolDefaultItem( aPageItem );
212  rItemPool.SetPoolDefaultItem( aPaperSizeItem );
213  rItemPool.SetPoolDefaultItem( aLRSpaceItem );
214  rItemPool.SetPoolDefaultItem( aULSpaceItem );
215  rItemPool.SetPoolDefaultItem( SfxUInt16Item( ATTR_PAGE_SCALE, 100 ) );
216  ScPageScaleToItem aScaleToItem;
217  rItemPool.SetPoolDefaultItem( aScaleToItem );
219  }
220  }
221  break;
222 
223  case SfxStyleFamily::Para:
224  default:
225  pSet = new SfxItemSet( GetPool()->GetPool(), svl::Items<ATTR_PATTERN_START, ATTR_PATTERN_END> );
226  break;
227  }
228  bMySet = true;
229  }
230  if ( nHelpId == HID_SC_SHEET_CELL_ERG1 )
231  {
232  if ( !pSet->Count() )
233  {
234  // Hack to work around that when this code is called from
235  // ~ScStyleSheetPool -> ~SfxStyleSheetPool, GetPool() is no longer
236  // an ScStyleSheetPool:
237  ScStyleSheetPool * pool = dynamic_cast<ScStyleSheetPool *>(
238  GetPool());
239  if (pool != nullptr) {
240  ScDocument* pDoc = pool->GetDocument();
241  if ( pDoc )
242  {
243  sal_uInt32 nNumFmt = pDoc->GetFormatTable()->GetStandardFormat( SvNumFormatType::CURRENCY,ScGlobal::eLnge );
244  pSet->Put( SfxUInt32Item( ATTR_VALUE_FORMAT, nNumFmt ) );
245  }
246  }
247  }
248  }
249 
250  return *pSet;
251 }
252 
254 {
255  if ( GetFamily() == SfxStyleFamily::Para )
256  {
257  // Always query the document to let it decide if a rescan is necessary,
258  // and store the state.
259  ScDocument* pDoc = static_cast<ScStyleSheetPool*>(m_pPool)->GetDocument();
260  if ( pDoc && pDoc->IsStyleSheetUsed( *this ) )
262  else
264  return eUsage == Usage::USED;
265  }
266  else
267  return true;
268 }
269 
271 {
272  if ( rHint.GetId() == SfxHintId::Dying )
273  GetItemSet().SetParent( nullptr );
274 }
275 
276 // Avoid creating a Style "Standard" if this is not the Standard-Name;
277 // otherwise two styles would have the same name when storing.
278 // (on loading the style is created directly per Make with the name; making this query
279 // not applicable)
280 //TODO: If at any time during loading SetName is called, a flag has to be set/checked for loading
281 //TODO: The whole check has to be removed if for a new file version the name transformation is dropped.
282 
283 bool ScStyleSheet::SetName(const OUString& rNew, bool bReindexNow)
284 {
285  OUString aFileStdName = STRING_STANDARD;
286  if ( rNew == aFileStdName && aFileStdName != ScResId(STR_STYLENAME_STANDARD) )
287  return false;
288  else
289  return SfxStyleSheet::SetName(rNew, bReindexNow);
290 }
291 
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool IsSystemRTL()
Definition: global.cxx:847
virtual std::unique_ptr< SfxStyleSheetIterator > CreateIterator(SfxStyleFamily, SfxStyleSearchBits nMask=SfxStyleSearchBits::All)
constexpr auto TWO_CM
Definition: stlsheet.cxx:45
constexpr TypedWhichId< SvxSizeItem > ATTR_PAGE_SIZE(161)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:89
SfxItemPool & GetPool()
constexpr TypedWhichId< SvxBoxItem > ATTR_BORDER(150)
virtual bool SetParent(const OUString &rParentName) override
Definition: stlsheet.cxx:90
static constexpr auto Items
virtual ~ScStyleSheet() override
Definition: stlsheet.cxx:64
constexpr TypedWhichId< SfxUInt16Item > ATTR_PAGE_SCALE(175)
void SetTable(bool bNew)
constexpr Point convert(const Point &rPoint, o3tl::Length eFrom, o3tl::Length eTo)
void SetLandscape(bool bL)
virtual bool SetName(const OUString &rNewName, bool bReindexNow=true)
virtual bool HasParentSupport() const override
Definition: stlsheet.cxx:73
constexpr TypedWhichId< SvxBoxInfoItem > ATTR_BORDER_INNER(151)
SvxFrameDirection
virtual SfxItemSet & GetItemSet()
SfxStyleFamily GetFamily() const
SfxHintId GetId() const
void RepaintRange(const ScRange &rRange)
Definition: documen8.cxx:691
const OUString & GetName() const
constexpr TypedWhichId< SvxShadowItem > ATTR_SHADOW(152)
const SCROW MAXROW
Definition: address.hxx:68
virtual bool SetName(const OUString &rNewName, bool bReindexNow=true) override
Definition: stlsheet.cxx:283
virtual bool HasFollowSupport() const override
Definition: stlsheet.cxx:68
SfxStyleFamily
SC_DLLPUBLIC void ResetParent()
Definition: stlsheet.cxx:123
UNKNOWN
virtual bool SetParent(const OUString &) override
static Size GetDefaultPaperSize(MapUnit eUnit=MapUnit::MapTwip)
SfxStyleSheetBasePool * GetPool()
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:441
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: stlsheet.cxx:270
ScDocument * GetDocument() const
Definition: stlpool.hxx:35
constexpr TypedWhichId< SvxSetItem > ATTR_PAGE_HEADERSET(184)
const SCCOL MAXCOL
Definition: address.hxx:69
SfxStyleSheetBasePool * m_pPool
sal_uInt16 Count() const
constexpr TypedWhichId< SfxUInt32Item > ATTR_VALUE_FORMAT(146)
static SC_DLLPUBLIC LanguageType eLnge
Definition: global.hxx:553
const SCTAB MAXTAB
Definition: address.hxx:70
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
constexpr TypedWhichId< SvxLRSpaceItem > ATTR_LRSPACE(157)
#define STRING_STANDARD
Definition: global.hxx:58
SfxStyleFamily nFamily
constexpr TypedWhichId< ScPageScaleToItem > ATTR_PAGE_SCALETO(188)
void SetParent(const SfxItemSet *pNew)
constexpr TypedWhichId< SvxBrushItem > ATTR_BACKGROUND(148)
bool IsStyleSheetUsed(const ScStyleSheet &rStyle) const
Definition: document.cxx:5006
constexpr TypedWhichId< SvxFrameDirectionItem > ATTR_WRITINGDIR(138)
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
Contains the "scale to width/height" attribute in page styles.
Definition: attrib.hxx:222
constexpr TypedWhichId< SvXMLAttrContainerItem > ATTR_USERDEF(122)
void SetWhich(sal_uInt16 nId)
sal_uInt32 GetStandardFormat(SvNumFormatType eType, LanguageType eLnge=LANGUAGE_DONTKNOW)
virtual SC_DLLPUBLIC SfxItemSet & GetItemSet() override
Definition: stlsheet.cxx:128
constexpr TypedWhichId< SvxULSpaceItem > ATTR_ULSPACE(158)
ScStyleSheet::Usage eUsage
Definition: stlsheet.hxx:67
constexpr TypedWhichId< SvxPageItem > ATTR_PAGE(159)
virtual bool IsUsed() const override
Definition: stlsheet.cxx:253
constexpr TypedWhichId< SfxUInt16Item > ATTR_PAGE_SCALETOPAGES(176)
void SetValid(SvxBoxInfoItemValidFlags nValid, bool bValid=true)
SfxItemSet * pSet
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All)
constexpr TypedWhichId< SvxSetItem > ATTR_PAGE_FOOTERSET(185)
ScStyleSheet(const ScStyleSheet &rStyle)
Definition: stlsheet.cxx:58
constexpr auto HFDIST_CM
Definition: stlsheet.cxx:46
SfxStyleSearchBits
void SetPoolDefaultItem(const SfxPoolItem &)
void SetDist(bool bNew)