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