1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <editeng/memberids.h>
21 #include <svl/hint.hxx>
22 #include <svl/itemprop.hxx>
23 #include <vcl/svapp.hxx>
24 #include <i18nlangtag/languagetag.hxx>
26 #include <scitems.hxx>
27 #include <defltuno.hxx>
28 #include <miscuno.hxx>
30 #include <docpool.hxx>
31 #include <unonames.hxx>
32 #include <docoptio.hxx>
37 using namespace ::com::sun::star
;
39 static std::span
<const SfxItemPropertyMapEntry
> lcl_GetDocDefaultsMap()
41 static const SfxItemPropertyMapEntry aDocDefaultsMap_Impl
[] =
43 { SC_UNONAME_CFCHARS
, ATTR_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_CHAR_SET
},
44 { SC_UNO_CJK_CFCHARS
, ATTR_CJK_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_CHAR_SET
},
45 { SC_UNO_CTL_CFCHARS
, ATTR_CTL_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_CHAR_SET
},
46 { SC_UNONAME_CFFAMIL
, ATTR_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_FAMILY
},
47 { SC_UNO_CJK_CFFAMIL
, ATTR_CJK_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_FAMILY
},
48 { SC_UNO_CTL_CFFAMIL
, ATTR_CTL_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_FAMILY
},
49 { SC_UNONAME_CFNAME
, ATTR_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_FAMILY_NAME
},
50 { SC_UNO_CJK_CFNAME
, ATTR_CJK_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_FAMILY_NAME
},
51 { SC_UNO_CTL_CFNAME
, ATTR_CTL_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_FAMILY_NAME
},
52 { SC_UNONAME_CFPITCH
, ATTR_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_PITCH
},
53 { SC_UNO_CJK_CFPITCH
, ATTR_CJK_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_PITCH
},
54 { SC_UNO_CTL_CFPITCH
, ATTR_CTL_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_PITCH
},
55 { SC_UNONAME_CFSTYLE
, ATTR_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_STYLE_NAME
},
56 { SC_UNO_CJK_CFSTYLE
, ATTR_CJK_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_STYLE_NAME
},
57 { SC_UNO_CTL_CFSTYLE
, ATTR_CTL_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_STYLE_NAME
},
58 { SC_UNONAME_CLOCAL
, ATTR_FONT_LANGUAGE
, cppu::UnoType
<lang::Locale
>::get(), 0, MID_LANG_LOCALE
},
59 { SC_UNO_CJK_CLOCAL
, ATTR_CJK_FONT_LANGUAGE
, cppu::UnoType
<lang::Locale
>::get(), 0, MID_LANG_LOCALE
},
60 { SC_UNO_CTL_CLOCAL
, ATTR_CTL_FONT_LANGUAGE
, cppu::UnoType
<lang::Locale
>::get(), 0, MID_LANG_LOCALE
},
61 { SC_UNONAME_CHEIGHT
, ATTR_FONT_HEIGHT
, cppu::UnoType
<float>::get(), 0, MID_FONTHEIGHT
| CONVERT_TWIPS
},
62 { SC_UNO_CJK_CHEIGHT
, ATTR_CJK_FONT_HEIGHT
, cppu::UnoType
<float>::get(), 0, MID_FONTHEIGHT
| CONVERT_TWIPS
},
63 { SC_UNO_CTL_CHEIGHT
, ATTR_CTL_FONT_HEIGHT
, cppu::UnoType
<float>::get(), 0, MID_FONTHEIGHT
| CONVERT_TWIPS
},
64 { SC_UNO_STANDARDDEC
, 0, cppu::UnoType
<sal_Int16
>::get(), 0, 0 },
65 { SC_UNO_TABSTOPDIS
, 0, cppu::UnoType
<sal_Int32
>::get(), 0, 0 },
67 return aDocDefaultsMap_Impl
;
70 using sc::TwipsToEvenHMM
;
72 SC_SIMPLE_SERVICE_INFO( ScDocDefaultsObj
, u
"ScDocDefaultsObj"_ustr
, u
"com.sun.star.sheet.Defaults"_ustr
)
74 ScDocDefaultsObj::ScDocDefaultsObj(ScDocShell
* pDocSh
) :
76 aPropertyMap(lcl_GetDocDefaultsMap())
78 pDocShell
->GetDocument().AddUnoObject(*this);
81 ScDocDefaultsObj::~ScDocDefaultsObj()
86 pDocShell
->GetDocument().RemoveUnoObject(*this);
89 void ScDocDefaultsObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
91 if ( rHint
.GetId() == SfxHintId::Dying
)
93 pDocShell
= nullptr; // document gone
97 void ScDocDefaultsObj::ItemsChanged()
101 //! if not in XML import, adjust row heights
102 const auto & rDoc
= pDocShell
->GetDocument();
103 pDocShell
->PostPaint(ScRange(0, 0, 0, rDoc
.MaxCol(), rDoc
.MaxRow(), MAXTAB
), PaintPartFlags::Grid
);
109 uno::Reference
<beans::XPropertySetInfo
> SAL_CALL
ScDocDefaultsObj::getPropertySetInfo()
111 SolarMutexGuard aGuard
;
112 static uno::Reference
<beans::XPropertySetInfo
> aRef
= new SfxItemPropertySetInfo(
117 void SAL_CALL
ScDocDefaultsObj::setPropertyValue(
118 const OUString
& aPropertyName
, const uno::Any
& aValue
)
120 SolarMutexGuard aGuard
;
123 throw uno::RuntimeException();
125 const SfxItemPropertyMapEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
127 throw beans::UnknownPropertyException(aPropertyName
);
130 if(aPropertyName
==SC_UNO_STANDARDDEC
)
132 ScDocument
& rDoc
= pDocShell
->GetDocument();
133 ScDocOptions
aDocOpt(rDoc
.GetDocOptions());
134 sal_Int16 nValue
= 0;
135 if (aValue
>>= nValue
)
137 aDocOpt
.SetStdPrecision(static_cast<sal_uInt16
> (nValue
));
138 rDoc
.SetDocOptions(aDocOpt
);
141 else if (aPropertyName
== SC_UNO_TABSTOPDIS
)
143 ScDocument
& rDoc
= pDocShell
->GetDocument();
144 ScDocOptions
aDocOpt(rDoc
.GetDocOptions());
145 sal_Int32 nValue
= 0;
146 if (aValue
>>= nValue
)
148 aDocOpt
.SetTabDistance(o3tl::toTwips(nValue
, o3tl::Length::mm100
));
149 rDoc
.SetDocOptions(aDocOpt
);
153 else if ( pEntry
->nWID
== ATTR_FONT_LANGUAGE
||
154 pEntry
->nWID
== ATTR_CJK_FONT_LANGUAGE
||
155 pEntry
->nWID
== ATTR_CTL_FONT_LANGUAGE
)
157 // for getPropertyValue the PoolDefaults are sufficient,
158 // but setPropertyValue has to be handled differently
160 lang::Locale aLocale
;
161 if ( aValue
>>= aLocale
)
164 if (!aLocale
.Language
.isEmpty() || !aLocale
.Country
.isEmpty())
165 eNew
= LanguageTag::convertToLanguageType( aLocale
, false);
167 eNew
= LANGUAGE_NONE
;
169 ScDocument
& rDoc
= pDocShell
->GetDocument();
170 LanguageType eLatin
, eCjk
, eCtl
;
171 rDoc
.GetLanguage( eLatin
, eCjk
, eCtl
);
173 if ( pEntry
->nWID
== ATTR_CJK_FONT_LANGUAGE
)
175 else if ( pEntry
->nWID
== ATTR_CTL_FONT_LANGUAGE
)
180 rDoc
.SetLanguage( eLatin
, eCjk
, eCtl
);
185 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
186 std::unique_ptr
<SfxPoolItem
> pNewItem(pPool
->GetUserOrPoolDefaultItem(pEntry
->nWID
).Clone());
188 if( !pNewItem
->PutValue( aValue
, pEntry
->nMemberId
) )
189 throw lang::IllegalArgumentException();
191 pPool
->SetUserDefaultItem( *pNewItem
);
197 uno::Any SAL_CALL
ScDocDefaultsObj::getPropertyValue( const OUString
& aPropertyName
)
199 // use pool default if set
201 SolarMutexGuard aGuard
;
204 throw uno::RuntimeException();
207 const SfxItemPropertyMapEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
209 throw beans::UnknownPropertyException(aPropertyName
);
213 if(aPropertyName
== SC_UNO_STANDARDDEC
)
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
);
224 else if (aPropertyName
== SC_UNO_TABSTOPDIS
)
226 ScDocument
& rDoc
= pDocShell
->GetDocument();
227 const ScDocOptions
& aDocOpt
= rDoc
.GetDocOptions();
228 sal_Int32
nValue (TwipsToEvenHMM(aDocOpt
.GetTabDistance()));
234 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
235 const SfxPoolItem
& rItem
= pPool
->GetUserOrPoolDefaultItem( pEntry
->nWID
);
236 rItem
.QueryValue( aRet
, pEntry
->nMemberId
);
241 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScDocDefaultsObj
)
245 beans::PropertyState SAL_CALL
ScDocDefaultsObj::getPropertyState( const OUString
& aPropertyName
)
247 SolarMutexGuard aGuard
;
250 throw uno::RuntimeException();
252 const SfxItemPropertyMapEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
254 throw beans::UnknownPropertyException(aPropertyName
);
256 beans::PropertyState eRet
= beans::PropertyState_DEFAULT_VALUE
;
258 sal_uInt16 nWID
= pEntry
->nWID
;
259 if ( nWID
== ATTR_FONT
|| nWID
== ATTR_CJK_FONT
|| nWID
== ATTR_CTL_FONT
|| !nWID
)
261 // static default for font is system-dependent,
262 // so font default is always treated as "direct value".
264 eRet
= beans::PropertyState_DIRECT_VALUE
;
268 // check if pool default is set
270 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
271 if ( pPool
->GetUserDefaultItem( nWID
) != nullptr )
272 eRet
= beans::PropertyState_DIRECT_VALUE
;
278 uno::Sequence
<beans::PropertyState
> SAL_CALL
ScDocDefaultsObj::getPropertyStates(
279 const uno::Sequence
<OUString
>& aPropertyNames
)
281 // the simple way: call getPropertyState
283 SolarMutexGuard aGuard
;
284 uno::Sequence
<beans::PropertyState
> aRet(aPropertyNames
.getLength());
285 std::transform(aPropertyNames
.begin(), aPropertyNames
.end(), aRet
.getArray(),
286 [this](const OUString
& rName
) -> beans::PropertyState
{ return getPropertyState(rName
); });
290 void SAL_CALL
ScDocDefaultsObj::setPropertyToDefault( const OUString
& aPropertyName
)
292 SolarMutexGuard aGuard
;
295 throw uno::RuntimeException();
297 const SfxItemPropertyMapEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
299 throw beans::UnknownPropertyException(aPropertyName
);
303 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
304 pPool
->ResetUserDefaultItem( pEntry
->nWID
);
310 uno::Any SAL_CALL
ScDocDefaultsObj::getPropertyDefault( const OUString
& aPropertyName
)
312 // always use static default
314 SolarMutexGuard aGuard
;
317 throw uno::RuntimeException();
319 const SfxItemPropertyMapEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
321 throw beans::UnknownPropertyException(aPropertyName
);
326 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
327 const SfxPoolItem
* pItem
= pPool
->GetPoolDefaultItem( pEntry
->nWID
);
329 pItem
->QueryValue( aRet
, pEntry
->nMemberId
);
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */