Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / unoobj / defltuno.cxx
blob5299528a46dfc85a3dee7344f84309eb01e6db0b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
32 #include "docsh.hxx"
33 #include "docpool.hxx"
34 #include "unonames.hxx"
35 #include "docoptio.hxx"
37 #include <limits>
39 using namespace ::com::sun::star;
41 //------------------------------------------------------------------------
43 static const SfxItemPropertyMapEntry* lcl_GetDocDefaultsMap()
45 static const SfxItemPropertyMapEntry aDocDefaultsMap_Impl[] =
47 {MAP_CHAR_LEN(SC_UNONAME_CFCHARS), ATTR_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_CHAR_SET },
48 {MAP_CHAR_LEN(SC_UNO_CJK_CFCHARS), ATTR_CJK_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_CHAR_SET },
49 {MAP_CHAR_LEN(SC_UNO_CTL_CFCHARS), ATTR_CTL_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_CHAR_SET },
50 {MAP_CHAR_LEN(SC_UNONAME_CFFAMIL), ATTR_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_FAMILY },
51 {MAP_CHAR_LEN(SC_UNO_CJK_CFFAMIL), ATTR_CJK_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_FAMILY },
52 {MAP_CHAR_LEN(SC_UNO_CTL_CFFAMIL), ATTR_CTL_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_FAMILY },
53 {MAP_CHAR_LEN(SC_UNONAME_CFNAME), ATTR_FONT, &getCppuType((OUString*)0), 0, MID_FONT_FAMILY_NAME },
54 {MAP_CHAR_LEN(SC_UNO_CJK_CFNAME), ATTR_CJK_FONT, &getCppuType((OUString*)0), 0, MID_FONT_FAMILY_NAME },
55 {MAP_CHAR_LEN(SC_UNO_CTL_CFNAME), ATTR_CTL_FONT, &getCppuType((OUString*)0), 0, MID_FONT_FAMILY_NAME },
56 {MAP_CHAR_LEN(SC_UNONAME_CFPITCH), ATTR_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_PITCH },
57 {MAP_CHAR_LEN(SC_UNO_CJK_CFPITCH), ATTR_CJK_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_PITCH },
58 {MAP_CHAR_LEN(SC_UNO_CTL_CFPITCH), ATTR_CTL_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_PITCH },
59 {MAP_CHAR_LEN(SC_UNONAME_CFSTYLE), ATTR_FONT, &getCppuType((OUString*)0), 0, MID_FONT_STYLE_NAME },
60 {MAP_CHAR_LEN(SC_UNO_CJK_CFSTYLE), ATTR_CJK_FONT, &getCppuType((OUString*)0), 0, MID_FONT_STYLE_NAME },
61 {MAP_CHAR_LEN(SC_UNO_CTL_CFSTYLE), ATTR_CTL_FONT, &getCppuType((OUString*)0), 0, MID_FONT_STYLE_NAME },
62 {MAP_CHAR_LEN(SC_UNONAME_CLOCAL), ATTR_FONT_LANGUAGE, &getCppuType((lang::Locale*)0), 0, MID_LANG_LOCALE },
63 {MAP_CHAR_LEN(SC_UNO_CJK_CLOCAL), ATTR_CJK_FONT_LANGUAGE, &getCppuType((lang::Locale*)0), 0, MID_LANG_LOCALE },
64 {MAP_CHAR_LEN(SC_UNO_CTL_CLOCAL), ATTR_CTL_FONT_LANGUAGE, &getCppuType((lang::Locale*)0), 0, MID_LANG_LOCALE },
65 {MAP_CHAR_LEN(SC_UNO_STANDARDDEC), 0, &getCppuType((sal_Int16*)0), 0, 0 },
66 {MAP_CHAR_LEN(SC_UNO_TABSTOPDIS), 0, &getCppuType((sal_Int32*)0), 0, 0 },
67 {0,0,0,0,0,0}
69 return aDocDefaultsMap_Impl;
72 using sc::HMMToTwips;
73 using sc::TwipsToHMM;
74 using sc::TwipsToEvenHMM;
76 //------------------------------------------------------------------------
78 SC_SIMPLE_SERVICE_INFO( ScDocDefaultsObj, "ScDocDefaultsObj", "com.sun.star.sheet.Defaults" )
80 //------------------------------------------------------------------------
82 ScDocDefaultsObj::ScDocDefaultsObj(ScDocShell* pDocSh) :
83 pDocShell( pDocSh ),
84 aPropertyMap(lcl_GetDocDefaultsMap())
86 pDocShell->GetDocument()->AddUnoObject(*this);
89 ScDocDefaultsObj::~ScDocDefaultsObj()
91 if (pDocShell)
92 pDocShell->GetDocument()->RemoveUnoObject(*this);
95 void ScDocDefaultsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
97 if ( rHint.ISA( SfxSimpleHint ) &&
98 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
100 pDocShell = NULL; // document gone
104 void ScDocDefaultsObj::ItemsChanged()
106 if (pDocShell)
108 //! if not in XML import, adjust row heights
110 pDocShell->PostPaint(ScRange(0, 0, 0, MAXCOL, MAXROW, MAXTAB), PAINT_GRID);
114 // XPropertySet
116 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScDocDefaultsObj::getPropertySetInfo()
117 throw(uno::RuntimeException)
119 SolarMutexGuard aGuard;
120 static uno::Reference<beans::XPropertySetInfo> aRef = new SfxItemPropertySetInfo(
121 aPropertyMap );
122 return aRef;
125 void SAL_CALL ScDocDefaultsObj::setPropertyValue(
126 const OUString& aPropertyName, const uno::Any& aValue )
127 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
128 lang::IllegalArgumentException, lang::WrappedTargetException,
129 uno::RuntimeException)
131 SolarMutexGuard aGuard;
133 if ( !pDocShell )
134 throw uno::RuntimeException();
136 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
137 if ( !pEntry )
138 throw beans::UnknownPropertyException();
139 if(!pEntry->nWID)
141 if(aPropertyName.equalsAscii(SC_UNO_STANDARDDEC) )
143 ScDocument* pDoc = pDocShell->GetDocument();
144 if (pDoc)
146 ScDocOptions aDocOpt(pDoc->GetDocOptions());
147 sal_Int16 nValue = 0;
148 if (aValue >>= nValue)
150 aDocOpt.SetStdPrecision(static_cast<sal_uInt16> (nValue));
151 pDoc->SetDocOptions(aDocOpt);
154 else
155 throw uno::RuntimeException();
157 else if (aPropertyName.equalsAscii(SC_UNO_TABSTOPDIS) )
159 ScDocument* pDoc = pDocShell->GetDocument();
160 if (pDoc)
162 ScDocOptions aDocOpt(pDoc->GetDocOptions());
163 sal_Int32 nValue = 0;
164 if (aValue >>= nValue)
166 aDocOpt.SetTabDistance(static_cast<sal_uInt16>(HMMToTwips(nValue)));
167 pDoc->SetDocOptions(aDocOpt);
170 else
171 throw uno::RuntimeException();
174 else if ( pEntry->nWID == ATTR_FONT_LANGUAGE ||
175 pEntry->nWID == ATTR_CJK_FONT_LANGUAGE ||
176 pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
178 // for getPropertyValue the PoolDefaults are sufficient,
179 // but setPropertyValue has to be handled differently
181 lang::Locale aLocale;
182 if ( aValue >>= aLocale )
184 LanguageType eNew;
185 if (!aLocale.Language.isEmpty() || !aLocale.Country.isEmpty())
186 eNew = LanguageTag::convertToLanguageType( aLocale, false);
187 else
188 eNew = LANGUAGE_NONE;
190 ScDocument* pDoc = pDocShell->GetDocument();
191 LanguageType eLatin, eCjk, eCtl;
192 pDoc->GetLanguage( eLatin, eCjk, eCtl );
194 if ( pEntry->nWID == ATTR_CJK_FONT_LANGUAGE )
195 eCjk = eNew;
196 else if ( pEntry->nWID == ATTR_CTL_FONT_LANGUAGE )
197 eCtl = eNew;
198 else
199 eLatin = eNew;
201 pDoc->SetLanguage( eLatin, eCjk, eCtl );
204 else
206 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
207 SfxPoolItem* pNewItem = pPool->GetDefaultItem(pEntry->nWID).Clone();
209 if( !pNewItem->PutValue( aValue, pEntry->nMemberId ) )
210 throw lang::IllegalArgumentException();
212 pPool->SetPoolDefaultItem( *pNewItem );
213 delete pNewItem; // copied in SetPoolDefaultItem
215 ItemsChanged();
219 uno::Any SAL_CALL ScDocDefaultsObj::getPropertyValue( const OUString& aPropertyName )
220 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
221 uno::RuntimeException)
223 // use pool default if set
225 SolarMutexGuard aGuard;
227 if ( !pDocShell )
228 throw uno::RuntimeException();
230 uno::Any aRet;
231 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
232 if ( !pEntry )
233 throw beans::UnknownPropertyException();
235 if (!pEntry->nWID)
237 if(aPropertyName.equalsAscii(SC_UNO_STANDARDDEC) )
239 ScDocument* pDoc = pDocShell->GetDocument();
240 if (pDoc)
242 const ScDocOptions& aDocOpt = pDoc->GetDocOptions();
243 sal_uInt16 nPrec = aDocOpt.GetStdPrecision();
244 // the max value of unsigned 16-bit integer is used as the flag
245 // value for unlimited precision, c.f.
246 // SvNumberFormatter::UNLIMITED_PRECISION.
247 if (nPrec <= ::std::numeric_limits<sal_Int16>::max())
248 aRet <<= static_cast<sal_Int16> (nPrec);
250 else
251 throw uno::RuntimeException();
253 else if (aPropertyName.equalsAscii(SC_UNO_TABSTOPDIS) )
255 ScDocument* pDoc = pDocShell->GetDocument();
256 if (pDoc)
258 const ScDocOptions& aDocOpt = pDoc->GetDocOptions();
259 sal_Int32 nValue (TwipsToEvenHMM(aDocOpt.GetTabDistance()));
260 aRet <<= nValue;
262 else
263 throw uno::RuntimeException();
266 else
268 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
269 const SfxPoolItem& rItem = pPool->GetDefaultItem( pEntry->nWID );
270 rItem.QueryValue( aRet, pEntry->nMemberId );
272 return aRet;
275 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScDocDefaultsObj )
277 // XPropertyState
279 beans::PropertyState SAL_CALL ScDocDefaultsObj::getPropertyState( const OUString& aPropertyName )
280 throw(beans::UnknownPropertyException, uno::RuntimeException)
282 SolarMutexGuard aGuard;
284 if ( !pDocShell )
285 throw uno::RuntimeException();
287 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
288 if ( !pEntry )
289 throw beans::UnknownPropertyException();
291 beans::PropertyState eRet = beans::PropertyState_DEFAULT_VALUE;
293 sal_uInt16 nWID = pEntry->nWID;
294 if ( nWID == ATTR_FONT || nWID == ATTR_CJK_FONT || nWID == ATTR_CTL_FONT || !nWID )
296 // static default for font is system-dependent,
297 // so font default is always treated as "direct value".
299 eRet = beans::PropertyState_DIRECT_VALUE;
301 else
303 // check if pool default is set
305 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
306 if ( pPool->GetPoolDefaultItem( nWID ) != NULL )
307 eRet = beans::PropertyState_DIRECT_VALUE;
310 return eRet;
313 uno::Sequence<beans::PropertyState> SAL_CALL ScDocDefaultsObj::getPropertyStates(
314 const uno::Sequence<OUString>& aPropertyNames )
315 throw(beans::UnknownPropertyException, uno::RuntimeException)
317 // the simple way: call getPropertyState
319 SolarMutexGuard aGuard;
320 const OUString* pNames = aPropertyNames.getConstArray();
321 uno::Sequence<beans::PropertyState> aRet(aPropertyNames.getLength());
322 beans::PropertyState* pStates = aRet.getArray();
323 for(sal_Int32 i = 0; i < aPropertyNames.getLength(); i++)
324 pStates[i] = getPropertyState(pNames[i]);
325 return aRet;
328 void SAL_CALL ScDocDefaultsObj::setPropertyToDefault( const OUString& aPropertyName )
329 throw(beans::UnknownPropertyException, uno::RuntimeException)
331 SolarMutexGuard aGuard;
333 if ( !pDocShell )
334 throw uno::RuntimeException();
336 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
337 if ( !pEntry )
338 throw beans::UnknownPropertyException();
340 if (pEntry->nWID)
342 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
343 pPool->ResetPoolDefaultItem( pEntry->nWID );
345 ItemsChanged();
349 uno::Any SAL_CALL ScDocDefaultsObj::getPropertyDefault( const OUString& aPropertyName )
350 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
351 uno::RuntimeException)
353 // always use static default
355 SolarMutexGuard aGuard;
357 if ( !pDocShell )
358 throw uno::RuntimeException();
360 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
361 if ( !pEntry )
362 throw beans::UnknownPropertyException();
364 uno::Any aRet;
365 if (pEntry->nWID)
367 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
368 const SfxPoolItem* pItem = pPool->GetItem2( pEntry->nWID, SFX_ITEMS_DEFAULT );
369 if (pItem)
370 pItem->QueryValue( aRet, pEntry->nMemberId );
372 return aRet;
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */