LibreOffice Module sc (master)  1
defltuno.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 <editeng/memberids.h>
21 #include <svl/hint.hxx>
22 #include <svl/itemprop.hxx>
23 #include <vcl/svapp.hxx>
25 
26 #include <scitems.hxx>
27 #include <defltuno.hxx>
28 #include <miscuno.hxx>
29 #include <docsh.hxx>
30 #include <docpool.hxx>
31 #include <unonames.hxx>
32 #include <docoptio.hxx>
33 
34 #include <limits>
35 
36 class SvxFontItem;
37 using namespace ::com::sun::star;
38 
40 {
41  static const SfxItemPropertyMapEntry aDocDefaultsMap_Impl[] =
42  {
63  { u"", 0, css::uno::Type(), 0, 0 }
64  };
65  return aDocDefaultsMap_Impl;
66 }
67 
68 using sc::HMMToTwips;
69 using sc::TwipsToEvenHMM;
70 
71 SC_SIMPLE_SERVICE_INFO( ScDocDefaultsObj, "ScDocDefaultsObj", "com.sun.star.sheet.Defaults" )
72 
74  pDocShell( pDocSh ),
75  aPropertyMap(lcl_GetDocDefaultsMap())
76 {
77  pDocShell->GetDocument().AddUnoObject(*this);
78 }
79 
81 {
83 
84  if (pDocShell)
86 }
87 
89 {
90  if ( rHint.GetId() == SfxHintId::Dying )
91  {
92  pDocShell = nullptr; // document gone
93  }
94 }
95 
97 {
98  if (pDocShell)
99  {
101  const auto & rDoc = pDocShell->GetDocument();
102  pDocShell->PostPaint(ScRange(0, 0, 0, rDoc.MaxCol(), rDoc.MaxRow(), MAXTAB), PaintPartFlags::Grid);
103  }
104 }
105 
106 // XPropertySet
107 
108 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScDocDefaultsObj::getPropertySetInfo()
109 {
110  SolarMutexGuard aGuard;
111  static uno::Reference<beans::XPropertySetInfo> aRef = new SfxItemPropertySetInfo(
112  aPropertyMap );
113  return aRef;
114 }
115 
117  const OUString& aPropertyName, const uno::Any& aValue )
118 {
119  SolarMutexGuard aGuard;
120 
121  if ( !pDocShell )
122  throw uno::RuntimeException();
123 
124  const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
125  if ( !pEntry )
126  throw beans::UnknownPropertyException(aPropertyName);
127  if(!pEntry->nWID)
128  {
129  if(aPropertyName ==SC_UNO_STANDARDDEC)
130  {
131  ScDocument& rDoc = pDocShell->GetDocument();
132  ScDocOptions aDocOpt(rDoc.GetDocOptions());
133  sal_Int16 nValue = 0;
134  if (aValue >>= nValue)
135  {
136  aDocOpt.SetStdPrecision(static_cast<sal_uInt16> (nValue));
137  rDoc.SetDocOptions(aDocOpt);
138  }
139  }
140  else if (aPropertyName == SC_UNO_TABSTOPDIS)
141  {
142  ScDocument& rDoc = pDocShell->GetDocument();
143  ScDocOptions aDocOpt(rDoc.GetDocOptions());
144  sal_Int32 nValue = 0;
145  if (aValue >>= nValue)
146  {
147  aDocOpt.SetTabDistance(static_cast<sal_uInt16>(HMMToTwips(nValue)));
148  rDoc.SetDocOptions(aDocOpt);
149  }
150  }
151  }
152  else if ( pEntry->nWID == ATTR_FONT_LANGUAGE ||
153  pEntry->nWID == ATTR_CJK_FONT_LANGUAGE ||
154  pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
155  {
156  // for getPropertyValue the PoolDefaults are sufficient,
157  // but setPropertyValue has to be handled differently
158 
159  lang::Locale aLocale;
160  if ( aValue >>= aLocale )
161  {
162  LanguageType eNew;
163  if (!aLocale.Language.isEmpty() || !aLocale.Country.isEmpty())
164  eNew = LanguageTag::convertToLanguageType( aLocale, false);
165  else
166  eNew = LANGUAGE_NONE;
167 
168  ScDocument& rDoc = pDocShell->GetDocument();
169  LanguageType eLatin, eCjk, eCtl;
170  rDoc.GetLanguage( eLatin, eCjk, eCtl );
171 
172  if ( pEntry->nWID == ATTR_CJK_FONT_LANGUAGE )
173  eCjk = eNew;
174  else if ( pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
175  eCtl = eNew;
176  else
177  eLatin = eNew;
178 
179  rDoc.SetLanguage( eLatin, eCjk, eCtl );
180  }
181  }
182  else
183  {
185  std::unique_ptr<SfxPoolItem> pNewItem(pPool->GetDefaultItem(pEntry->nWID).Clone());
186 
187  if( !pNewItem->PutValue( aValue, pEntry->nMemberId ) )
188  throw lang::IllegalArgumentException();
189 
190  pPool->SetPoolDefaultItem( *pNewItem );
191 
192  ItemsChanged();
193  }
194 }
195 
196 uno::Any SAL_CALL ScDocDefaultsObj::getPropertyValue( const OUString& aPropertyName )
197 {
198  // use pool default if set
199 
200  SolarMutexGuard aGuard;
201 
202  if ( !pDocShell )
203  throw uno::RuntimeException();
204 
205  uno::Any aRet;
206  const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
207  if ( !pEntry )
208  throw beans::UnknownPropertyException(aPropertyName);
209 
210  if (!pEntry->nWID)
211  {
212  if(aPropertyName == SC_UNO_STANDARDDEC)
213  {
214  ScDocument& rDoc = pDocShell->GetDocument();
215  const ScDocOptions& aDocOpt = rDoc.GetDocOptions();
216  sal_uInt16 nPrec = aDocOpt.GetStdPrecision();
217  // the max value of unsigned 16-bit integer is used as the flag
218  // value for unlimited precision, c.f.
219  // SvNumberFormatter::UNLIMITED_PRECISION.
220  if (nPrec <= ::std::numeric_limits<sal_Int16>::max())
221  aRet <<= static_cast<sal_Int16> (nPrec);
222  }
223  else if (aPropertyName == SC_UNO_TABSTOPDIS)
224  {
225  ScDocument& rDoc = pDocShell->GetDocument();
226  const ScDocOptions& aDocOpt = rDoc.GetDocOptions();
227  sal_Int32 nValue (TwipsToEvenHMM(aDocOpt.GetTabDistance()));
228  aRet <<= nValue;
229  }
230  }
231  else
232  {
234  const SfxPoolItem& rItem = pPool->GetDefaultItem( pEntry->nWID );
235  rItem.QueryValue( aRet, pEntry->nMemberId );
236  }
237  return aRet;
238 }
239 
241 
242 // XPropertyState
243 
244 beans::PropertyState SAL_CALL ScDocDefaultsObj::getPropertyState( const OUString& aPropertyName )
245 {
246  SolarMutexGuard aGuard;
247 
248  if ( !pDocShell )
249  throw uno::RuntimeException();
250 
251  const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
252  if ( !pEntry )
253  throw beans::UnknownPropertyException(aPropertyName);
254 
255  beans::PropertyState eRet = beans::PropertyState_DEFAULT_VALUE;
256 
257  sal_uInt16 nWID = pEntry->nWID;
258  if ( nWID == ATTR_FONT || nWID == ATTR_CJK_FONT || nWID == ATTR_CTL_FONT || !nWID )
259  {
260  // static default for font is system-dependent,
261  // so font default is always treated as "direct value".
262 
263  eRet = beans::PropertyState_DIRECT_VALUE;
264  }
265  else
266  {
267  // check if pool default is set
268 
269  ScDocumentPool* pPool = pDocShell->GetDocument().GetPool();
270  if ( pPool->GetPoolDefaultItem( nWID ) != nullptr )
271  eRet = beans::PropertyState_DIRECT_VALUE;
272  }
273 
274  return eRet;
275 }
276 
277 uno::Sequence<beans::PropertyState> SAL_CALL ScDocDefaultsObj::getPropertyStates(
278  const uno::Sequence<OUString>& aPropertyNames )
279 {
280  // the simple way: call getPropertyState
281 
282  SolarMutexGuard aGuard;
283  uno::Sequence<beans::PropertyState> aRet(aPropertyNames.getLength());
284  std::transform(aPropertyNames.begin(), aPropertyNames.end(), aRet.begin(),
285  [this](const OUString& rName) -> beans::PropertyState { return getPropertyState(rName); });
286  return aRet;
287 }
288 
289 void SAL_CALL ScDocDefaultsObj::setPropertyToDefault( const OUString& aPropertyName )
290 {
291  SolarMutexGuard aGuard;
292 
293  if ( !pDocShell )
294  throw uno::RuntimeException();
295 
296  const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
297  if ( !pEntry )
298  throw beans::UnknownPropertyException(aPropertyName);
299 
300  if (pEntry->nWID)
301  {
303  pPool->ResetPoolDefaultItem( pEntry->nWID );
304 
305  ItemsChanged();
306  }
307 }
308 
309 uno::Any SAL_CALL ScDocDefaultsObj::getPropertyDefault( const OUString& aPropertyName )
310 {
311  // always use static default
312 
313  SolarMutexGuard aGuard;
314 
315  if ( !pDocShell )
316  throw uno::RuntimeException();
317 
318  const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
319  if ( !pEntry )
320  throw beans::UnknownPropertyException(aPropertyName);
321 
322  uno::Any aRet;
323  if (pEntry->nWID)
324  {
326  const SfxPoolItem* pItem = pPool->GetItem2Default( pEntry->nWID );
327  if (pItem)
328  pItem->QueryValue( aRet, pEntry->nMemberId );
329  }
330  return aRet;
331 }
332 
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SC_UNO_CJK_CFSTYLE
Definition: unonames.hxx:77
const SfxItemPropertySimpleEntry * getByName(std::u16string_view rName) const
#define LANGUAGE_NONE
constexpr TypedWhichId< SvxLanguageItem > ATTR_CJK_FONT_LANGUAGE(115)
virtual ~ScDocDefaultsObj() override
Definition: defltuno.cxx:80
#define SC_UNO_CTL_CFFAMIL
Definition: unonames.hxx:88
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const =0
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
Definition: defltuno.cxx:196
const SfxPoolItem * GetPoolDefaultItem(sal_uInt16 nWhich) const
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
constexpr TypedWhichId< SvxLanguageItem > ATTR_FONT_LANGUAGE(110)
#define SC_UNO_CJK_CFNAME
Definition: unonames.hxx:76
#define SC_UNO_TABSTOPDIS
Definition: unonames.hxx:501
constexpr TypedWhichId< SvxFontItem > ATTR_FONT(100)
const SfxPoolItem * GetItem2Default(sal_uInt16 nWhich) const
SfxHintId GetId() const
#define SC_UNO_CJK_CLOCAL
Definition: unonames.hxx:84
SC_DLLPUBLIC ScDocumentPool * GetPool()
Definition: document.cxx:6054
#define SC_UNO_CJK_CFPITCH
Definition: unonames.hxx:80
#define MID_FONT_FAMILY
#define SC_UNO_CTL_CFCHARS
Definition: unonames.hxx:89
#define MID_FONT_FAMILY_NAME
#define SC_UNO_CJK_CFFAMIL
Definition: unonames.hxx:78
#define MID_LANG_LOCALE
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const
static const SfxItemPropertyMapEntry * lcl_GetDocDefaultsMap()
Definition: defltuno.cxx:39
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
Definition: defltuno.cxx:116
virtual css::uno::Sequence< css::beans::PropertyState > SAL_CALL getPropertyStates(const css::uno::Sequence< OUString > &aPropertyName) override
Definition: defltuno.cxx:277
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: defltuno.cxx:108
#define SC_UNO_CTL_CFNAME
Definition: unonames.hxx:86
#define SC_UNONAME_CFNAME
Definition: unonames.hxx:68
#define SC_UNO_CTL_CFSTYLE
Definition: unonames.hxx:87
SC_DLLPUBLIC const ScDocOptions & GetDocOptions() const
Definition: documen3.cxx:1922
#define SC_UNONAME_CFPITCH
Definition: unonames.hxx:72
#define SC_UNONAME_CFCHARS
Definition: unonames.hxx:71
sal_uInt16 GetTabDistance() const
Definition: docoptio.hxx:71
#define SC_UNONAME_CLOCAL
Definition: unonames.hxx:63
ScDocShell * pDocShell
Definition: defltuno.hxx:39
float u
inline::tools::Long HMMToTwips(::tools::Long nHMM)
Definition: global.hxx:111
css::uno::Type const & get()
#define SC_SIMPLE_SERVICE_INFO(ClassName, ClassNameAscii, ServiceAscii)
Definition: miscuno.hxx:64
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
Definition: defltuno.cxx:88
const SCTAB MAXTAB
Definition: address.hxx:71
constexpr TypedWhichId< SvxFontItem > ATTR_CTL_FONT(116)
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
void PostPaint(SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab, SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab, PaintPartFlags nPart, sal_uInt16 nExtFlags=0)
Definition: docsh3.cxx:98
#define MID_FONT_PITCH
#define SC_IMPL_DUMMY_PROPERTY_LISTENER(ClassName)
Definition: miscuno.hxx:73
constexpr TypedWhichId< SvxFontItem > ATTR_CJK_FONT(111)
#define SC_UNONAME_CFSTYLE
Definition: unonames.hxx:69
#define SC_UNO_STANDARDDEC
Definition: unonames.hxx:510
constexpr TypedWhichId< SvxLanguageItem > ATTR_CTL_FONT_LANGUAGE(120)
virtual css::beans::PropertyState SAL_CALL getPropertyState(const OUString &PropertyName) override
Definition: defltuno.cxx:244
#define MID_FONT_STYLE_NAME
SC_DLLPUBLIC void SetDocOptions(const ScDocOptions &rOpt)
Definition: documen3.cxx:1928
void ResetPoolDefaultItem(sal_uInt16 nWhich)
#define SC_UNONAME_CFFAMIL
Definition: unonames.hxx:70
void RemoveUnoObject(SfxListener &rObject)
Definition: documen3.cxx:900
void SetLanguage(LanguageType eLatin, LanguageType eCjk, LanguageType eCtl)
Definition: documen3.cxx:1955
SfxItemPropertyMap aPropertyMap
Definition: defltuno.hxx:40
const ScDocument & GetDocument() const
Definition: docsh.hxx:217
sal_uInt16 GetStdPrecision() const
Definition: docoptio.hxx:79
inline::tools::Long TwipsToEvenHMM(::tools::Long nTwips)
Definition: global.hxx:112
virtual css::uno::Any SAL_CALL getPropertyDefault(const OUString &aPropertyName) override
Definition: defltuno.cxx:309
virtual void SAL_CALL setPropertyToDefault(const OUString &PropertyName) override
Definition: defltuno.cxx:289
SC_DLLPUBLIC void GetLanguage(LanguageType &rLatin, LanguageType &rCjk, LanguageType &rCtl) const
Definition: documen3.cxx:1948
void ItemsChanged()
Definition: defltuno.cxx:96
#define SC_UNO_CTL_CFPITCH
Definition: unonames.hxx:90
#define SC_UNO_CJK_CFCHARS
Definition: unonames.hxx:79
sal_Int16 nValue
void SetPoolDefaultItem(const SfxPoolItem &)
#define MID_FONT_CHAR_SET
#define SC_UNO_CTL_CLOCAL
Definition: unonames.hxx:94