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.hrc>
21 #include <svl/smplhint.hxx>
22 #include <svl/itemprop.hxx>
23 #include <svx/unomid.hxx>
24 #include <vcl/svapp.hxx>
25 #include <i18nlangtag/languagetag.hxx>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include "scitems.hxx"
30 #include "defltuno.hxx"
31 #include "miscuno.hxx"
33 #include "docpool.hxx"
34 #include "unonames.hxx"
35 #include "docoptio.hxx"
39 using namespace ::com::sun::star
;
41 static const SfxItemPropertyMapEntry
* lcl_GetDocDefaultsMap()
43 static const SfxItemPropertyMapEntry aDocDefaultsMap_Impl
[] =
45 {OUString(SC_UNONAME_CFCHARS
), ATTR_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_CHAR_SET
},
46 {OUString(SC_UNO_CJK_CFCHARS
), ATTR_CJK_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_CHAR_SET
},
47 {OUString(SC_UNO_CTL_CFCHARS
), ATTR_CTL_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_CHAR_SET
},
48 {OUString(SC_UNONAME_CFFAMIL
), ATTR_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_FAMILY
},
49 {OUString(SC_UNO_CJK_CFFAMIL
), ATTR_CJK_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_FAMILY
},
50 {OUString(SC_UNO_CTL_CFFAMIL
), ATTR_CTL_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_FAMILY
},
51 {OUString(SC_UNONAME_CFNAME
), ATTR_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_FAMILY_NAME
},
52 {OUString(SC_UNO_CJK_CFNAME
), ATTR_CJK_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_FAMILY_NAME
},
53 {OUString(SC_UNO_CTL_CFNAME
), ATTR_CTL_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_FAMILY_NAME
},
54 {OUString(SC_UNONAME_CFPITCH
), ATTR_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_PITCH
},
55 {OUString(SC_UNO_CJK_CFPITCH
), ATTR_CJK_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_PITCH
},
56 {OUString(SC_UNO_CTL_CFPITCH
), ATTR_CTL_FONT
, cppu::UnoType
<sal_Int16
>::get(), 0, MID_FONT_PITCH
},
57 {OUString(SC_UNONAME_CFSTYLE
), ATTR_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_STYLE_NAME
},
58 {OUString(SC_UNO_CJK_CFSTYLE
), ATTR_CJK_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_STYLE_NAME
},
59 {OUString(SC_UNO_CTL_CFSTYLE
), ATTR_CTL_FONT
, cppu::UnoType
<OUString
>::get(), 0, MID_FONT_STYLE_NAME
},
60 {OUString(SC_UNONAME_CLOCAL
), ATTR_FONT_LANGUAGE
, cppu::UnoType
<lang::Locale
>::get(), 0, MID_LANG_LOCALE
},
61 {OUString(SC_UNO_CJK_CLOCAL
), ATTR_CJK_FONT_LANGUAGE
, cppu::UnoType
<lang::Locale
>::get(), 0, MID_LANG_LOCALE
},
62 {OUString(SC_UNO_CTL_CLOCAL
), ATTR_CTL_FONT_LANGUAGE
, cppu::UnoType
<lang::Locale
>::get(), 0, MID_LANG_LOCALE
},
63 {OUString(SC_UNO_STANDARDDEC
), 0, cppu::UnoType
<sal_Int16
>::get(), 0, 0 },
64 {OUString(SC_UNO_TABSTOPDIS
), 0, cppu::UnoType
<sal_Int32
>::get(), 0, 0 },
65 { OUString(), 0, css::uno::Type(), 0, 0 }
67 return aDocDefaultsMap_Impl
;
72 using sc::TwipsToEvenHMM
;
74 SC_SIMPLE_SERVICE_INFO( ScDocDefaultsObj
, "ScDocDefaultsObj", "com.sun.star.sheet.Defaults" )
76 ScDocDefaultsObj::ScDocDefaultsObj(ScDocShell
* pDocSh
) :
78 aPropertyMap(lcl_GetDocDefaultsMap())
80 pDocShell
->GetDocument().AddUnoObject(*this);
83 ScDocDefaultsObj::~ScDocDefaultsObj()
88 pDocShell
->GetDocument().RemoveUnoObject(*this);
91 void ScDocDefaultsObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
93 const SfxSimpleHint
* pSimpleHint
= dynamic_cast<const SfxSimpleHint
*>(&rHint
);
94 if ( pSimpleHint
&& pSimpleHint
->GetId() == SFX_HINT_DYING
)
96 pDocShell
= NULL
; // document gone
100 void ScDocDefaultsObj::ItemsChanged()
104 //! if not in XML import, adjust row heights
106 pDocShell
->PostPaint(ScRange(0, 0, 0, MAXCOL
, MAXROW
, MAXTAB
), PAINT_GRID
);
112 uno::Reference
<beans::XPropertySetInfo
> SAL_CALL
ScDocDefaultsObj::getPropertySetInfo()
113 throw(uno::RuntimeException
, std::exception
)
115 SolarMutexGuard aGuard
;
116 static uno::Reference
<beans::XPropertySetInfo
> aRef
= new SfxItemPropertySetInfo(
121 void SAL_CALL
ScDocDefaultsObj::setPropertyValue(
122 const OUString
& aPropertyName
, const uno::Any
& aValue
)
123 throw(beans::UnknownPropertyException
, beans::PropertyVetoException
,
124 lang::IllegalArgumentException
, lang::WrappedTargetException
,
125 uno::RuntimeException
, std::exception
)
127 SolarMutexGuard aGuard
;
130 throw uno::RuntimeException();
132 const SfxItemPropertySimpleEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
134 throw beans::UnknownPropertyException();
137 if(aPropertyName
==SC_UNO_STANDARDDEC
)
139 ScDocument
& rDoc
= pDocShell
->GetDocument();
140 ScDocOptions
aDocOpt(rDoc
.GetDocOptions());
141 sal_Int16 nValue
= 0;
142 if (aValue
>>= nValue
)
144 aDocOpt
.SetStdPrecision(static_cast<sal_uInt16
> (nValue
));
145 rDoc
.SetDocOptions(aDocOpt
);
148 else if (aPropertyName
== SC_UNO_TABSTOPDIS
)
150 ScDocument
& rDoc
= pDocShell
->GetDocument();
151 ScDocOptions
aDocOpt(rDoc
.GetDocOptions());
152 sal_Int32 nValue
= 0;
153 if (aValue
>>= nValue
)
155 aDocOpt
.SetTabDistance(static_cast<sal_uInt16
>(HMMToTwips(nValue
)));
156 rDoc
.SetDocOptions(aDocOpt
);
160 else if ( pEntry
->nWID
== ATTR_FONT_LANGUAGE
||
161 pEntry
->nWID
== ATTR_CJK_FONT_LANGUAGE
||
162 pEntry
->nWID
== ATTR_CTL_FONT_LANGUAGE
)
164 // for getPropertyValue the PoolDefaults are sufficient,
165 // but setPropertyValue has to be handled differently
167 lang::Locale aLocale
;
168 if ( aValue
>>= aLocale
)
171 if (!aLocale
.Language
.isEmpty() || !aLocale
.Country
.isEmpty())
172 eNew
= LanguageTag::convertToLanguageType( aLocale
, false);
174 eNew
= LANGUAGE_NONE
;
176 ScDocument
& rDoc
= pDocShell
->GetDocument();
177 LanguageType eLatin
, eCjk
, eCtl
;
178 rDoc
.GetLanguage( eLatin
, eCjk
, eCtl
);
180 if ( pEntry
->nWID
== ATTR_CJK_FONT_LANGUAGE
)
182 else if ( pEntry
->nWID
== ATTR_CTL_FONT_LANGUAGE
)
187 rDoc
.SetLanguage( eLatin
, eCjk
, eCtl
);
192 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
193 SfxPoolItem
* pNewItem
= pPool
->GetDefaultItem(pEntry
->nWID
).Clone();
195 if( !pNewItem
->PutValue( aValue
, pEntry
->nMemberId
) )
196 throw lang::IllegalArgumentException();
198 pPool
->SetPoolDefaultItem( *pNewItem
);
199 delete pNewItem
; // copied in SetPoolDefaultItem
205 uno::Any SAL_CALL
ScDocDefaultsObj::getPropertyValue( const OUString
& aPropertyName
)
206 throw(beans::UnknownPropertyException
, lang::WrappedTargetException
,
207 uno::RuntimeException
, std::exception
)
209 // use pool default if set
211 SolarMutexGuard aGuard
;
214 throw uno::RuntimeException();
217 const SfxItemPropertySimpleEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
219 throw beans::UnknownPropertyException();
223 if(aPropertyName
== SC_UNO_STANDARDDEC
)
225 ScDocument
& rDoc
= pDocShell
->GetDocument();
226 const ScDocOptions
& aDocOpt
= rDoc
.GetDocOptions();
227 sal_uInt16 nPrec
= aDocOpt
.GetStdPrecision();
228 // the max value of unsigned 16-bit integer is used as the flag
229 // value for unlimited precision, c.f.
230 // SvNumberFormatter::UNLIMITED_PRECISION.
231 if (nPrec
<= ::std::numeric_limits
<sal_Int16
>::max())
232 aRet
<<= static_cast<sal_Int16
> (nPrec
);
234 else if (aPropertyName
== SC_UNO_TABSTOPDIS
)
236 ScDocument
& rDoc
= pDocShell
->GetDocument();
237 const ScDocOptions
& aDocOpt
= rDoc
.GetDocOptions();
238 sal_Int32
nValue (TwipsToEvenHMM(aDocOpt
.GetTabDistance()));
244 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
245 const SfxPoolItem
& rItem
= pPool
->GetDefaultItem( pEntry
->nWID
);
246 rItem
.QueryValue( aRet
, pEntry
->nMemberId
);
251 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScDocDefaultsObj
)
255 beans::PropertyState SAL_CALL
ScDocDefaultsObj::getPropertyState( const OUString
& aPropertyName
)
256 throw(beans::UnknownPropertyException
, uno::RuntimeException
, std::exception
)
258 SolarMutexGuard aGuard
;
261 throw uno::RuntimeException();
263 const SfxItemPropertySimpleEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
265 throw beans::UnknownPropertyException();
267 beans::PropertyState eRet
= beans::PropertyState_DEFAULT_VALUE
;
269 sal_uInt16 nWID
= pEntry
->nWID
;
270 if ( nWID
== ATTR_FONT
|| nWID
== ATTR_CJK_FONT
|| nWID
== ATTR_CTL_FONT
|| !nWID
)
272 // static default for font is system-dependent,
273 // so font default is always treated as "direct value".
275 eRet
= beans::PropertyState_DIRECT_VALUE
;
279 // check if pool default is set
281 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
282 if ( pPool
->GetPoolDefaultItem( nWID
) != NULL
)
283 eRet
= beans::PropertyState_DIRECT_VALUE
;
289 uno::Sequence
<beans::PropertyState
> SAL_CALL
ScDocDefaultsObj::getPropertyStates(
290 const uno::Sequence
<OUString
>& aPropertyNames
)
291 throw(beans::UnknownPropertyException
, uno::RuntimeException
, std::exception
)
293 // the simple way: call getPropertyState
295 SolarMutexGuard aGuard
;
296 const OUString
* pNames
= aPropertyNames
.getConstArray();
297 uno::Sequence
<beans::PropertyState
> aRet(aPropertyNames
.getLength());
298 beans::PropertyState
* pStates
= aRet
.getArray();
299 for(sal_Int32 i
= 0; i
< aPropertyNames
.getLength(); i
++)
300 pStates
[i
] = getPropertyState(pNames
[i
]);
304 void SAL_CALL
ScDocDefaultsObj::setPropertyToDefault( const OUString
& aPropertyName
)
305 throw(beans::UnknownPropertyException
, uno::RuntimeException
, std::exception
)
307 SolarMutexGuard aGuard
;
310 throw uno::RuntimeException();
312 const SfxItemPropertySimpleEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
314 throw beans::UnknownPropertyException();
318 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
319 pPool
->ResetPoolDefaultItem( pEntry
->nWID
);
325 uno::Any SAL_CALL
ScDocDefaultsObj::getPropertyDefault( const OUString
& aPropertyName
)
326 throw(beans::UnknownPropertyException
, lang::WrappedTargetException
,
327 uno::RuntimeException
, std::exception
)
329 // always use static default
331 SolarMutexGuard aGuard
;
334 throw uno::RuntimeException();
336 const SfxItemPropertySimpleEntry
* pEntry
= aPropertyMap
.getByName( aPropertyName
);
338 throw beans::UnknownPropertyException();
343 ScDocumentPool
* pPool
= pDocShell
->GetDocument().GetPool();
344 const SfxPoolItem
* pItem
= pPool
->GetItem2Default( pEntry
->nWID
);
346 pItem
->QueryValue( aRet
, pEntry
->nMemberId
);
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */