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