sync master with lastest vba changes
[ooovba.git] / sc / source / ui / unoobj / defltuno.cxx
blob97bfe1aa3f4080afe6a97e6d4303c21dc9690cb3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: defltuno.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 #include <svtools/smplhint.hxx>
37 #include <svtools/itemprop.hxx>
38 #include <svx/unomid.hxx>
39 #include <i18npool/mslangid.hxx>
41 #include <com/sun/star/beans/PropertyAttribute.hpp>
43 #include "scitems.hxx"
44 #include "defltuno.hxx"
45 #include "miscuno.hxx"
46 #include "docsh.hxx"
47 #include "docpool.hxx"
48 #include "unoguard.hxx"
49 #include "unonames.hxx"
50 #include "docoptio.hxx"
52 using namespace ::com::sun::star;
54 //------------------------------------------------------------------------
56 const SfxItemPropertyMap* lcl_GetDocDefaultsMap()
58 static SfxItemPropertyMap aDocDefaultsMap_Impl[] =
60 {MAP_CHAR_LEN(SC_UNONAME_CFCHARS), ATTR_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_CHAR_SET },
61 {MAP_CHAR_LEN(SC_UNO_CJK_CFCHARS), ATTR_CJK_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_CHAR_SET },
62 {MAP_CHAR_LEN(SC_UNO_CTL_CFCHARS), ATTR_CTL_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_CHAR_SET },
63 {MAP_CHAR_LEN(SC_UNONAME_CFFAMIL), ATTR_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_FAMILY },
64 {MAP_CHAR_LEN(SC_UNO_CJK_CFFAMIL), ATTR_CJK_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_FAMILY },
65 {MAP_CHAR_LEN(SC_UNO_CTL_CFFAMIL), ATTR_CTL_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_FAMILY },
66 {MAP_CHAR_LEN(SC_UNONAME_CFNAME), ATTR_FONT, &getCppuType((rtl::OUString*)0), 0, MID_FONT_FAMILY_NAME },
67 {MAP_CHAR_LEN(SC_UNO_CJK_CFNAME), ATTR_CJK_FONT, &getCppuType((rtl::OUString*)0), 0, MID_FONT_FAMILY_NAME },
68 {MAP_CHAR_LEN(SC_UNO_CTL_CFNAME), ATTR_CTL_FONT, &getCppuType((rtl::OUString*)0), 0, MID_FONT_FAMILY_NAME },
69 {MAP_CHAR_LEN(SC_UNONAME_CFPITCH), ATTR_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_PITCH },
70 {MAP_CHAR_LEN(SC_UNO_CJK_CFPITCH), ATTR_CJK_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_PITCH },
71 {MAP_CHAR_LEN(SC_UNO_CTL_CFPITCH), ATTR_CTL_FONT, &getCppuType((sal_Int16*)0), 0, MID_FONT_PITCH },
72 {MAP_CHAR_LEN(SC_UNONAME_CFSTYLE), ATTR_FONT, &getCppuType((rtl::OUString*)0), 0, MID_FONT_STYLE_NAME },
73 {MAP_CHAR_LEN(SC_UNO_CJK_CFSTYLE), ATTR_CJK_FONT, &getCppuType((rtl::OUString*)0), 0, MID_FONT_STYLE_NAME },
74 {MAP_CHAR_LEN(SC_UNO_CTL_CFSTYLE), ATTR_CTL_FONT, &getCppuType((rtl::OUString*)0), 0, MID_FONT_STYLE_NAME },
75 {MAP_CHAR_LEN(SC_UNONAME_CLOCAL), ATTR_FONT_LANGUAGE, &getCppuType((lang::Locale*)0), 0, MID_LANG_LOCALE },
76 {MAP_CHAR_LEN(SC_UNO_CJK_CLOCAL), ATTR_CJK_FONT_LANGUAGE, &getCppuType((lang::Locale*)0), 0, MID_LANG_LOCALE },
77 {MAP_CHAR_LEN(SC_UNO_CTL_CLOCAL), ATTR_CTL_FONT_LANGUAGE, &getCppuType((lang::Locale*)0), 0, MID_LANG_LOCALE },
78 {MAP_CHAR_LEN(SC_UNO_STANDARDDEC), 0, &getCppuType((sal_Int16*)0), 0, 0 },
79 {MAP_CHAR_LEN(SC_UNO_TABSTOPDIS), 0, &getCppuType((sal_Int32*)0), 0, 0 },
80 {0,0,0,0,0,0}
82 return aDocDefaultsMap_Impl;
85 inline long TwipsToHMM(long nTwips) { return (nTwips * 127 + 36) / 72; }
86 inline long HMMToTwips(long nHMM) { return (nHMM * 72 + 63) / 127; }
87 inline long TwipsToEvenHMM(long nTwips) { return ( (nTwips * 127 + 72) / 144 ) * 2; }
89 //------------------------------------------------------------------------
91 SC_SIMPLE_SERVICE_INFO( ScDocDefaultsObj, "ScDocDefaultsObj", "com.sun.star.sheet.Defaults" )
93 //------------------------------------------------------------------------
95 ScDocDefaultsObj::ScDocDefaultsObj(ScDocShell* pDocSh) :
96 pDocShell( pDocSh )
98 pDocShell->GetDocument()->AddUnoObject(*this);
101 ScDocDefaultsObj::~ScDocDefaultsObj()
103 if (pDocShell)
104 pDocShell->GetDocument()->RemoveUnoObject(*this);
107 void ScDocDefaultsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
109 if ( rHint.ISA( SfxSimpleHint ) &&
110 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
112 pDocShell = NULL; // document gone
116 void ScDocDefaultsObj::ItemsChanged()
118 if (pDocShell)
120 //! if not in XML import, adjust row heights
122 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
126 // XPropertySet
128 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScDocDefaultsObj::getPropertySetInfo()
129 throw(uno::RuntimeException)
131 ScUnoGuard aGuard;
132 static uno::Reference<beans::XPropertySetInfo> aRef(
133 new SfxItemPropertySetInfo( lcl_GetDocDefaultsMap() ));
134 return aRef;
137 void SAL_CALL ScDocDefaultsObj::setPropertyValue(
138 const rtl::OUString& aPropertyName, const uno::Any& aValue )
139 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
140 lang::IllegalArgumentException, lang::WrappedTargetException,
141 uno::RuntimeException)
143 ScUnoGuard aGuard;
145 if ( !pDocShell )
146 throw uno::RuntimeException();
148 const SfxItemPropertyMap* pMap =
149 SfxItemPropertyMap::GetByName( lcl_GetDocDefaultsMap(), aPropertyName );
150 if ( !pMap )
151 throw beans::UnknownPropertyException();
152 if(!pMap->nWID)
154 if(aPropertyName.compareToAscii(SC_UNO_STANDARDDEC) == 0)
156 ScDocument* pDoc = pDocShell->GetDocument();
157 if (pDoc)
159 ScDocOptions aDocOpt(pDoc->GetDocOptions());
160 sal_Int16 nValue = 0;
161 if (aValue >>= nValue)
163 aDocOpt.SetStdPrecision(static_cast<sal_uInt8> (nValue));
164 pDoc->SetDocOptions(aDocOpt);
167 else
168 throw uno::RuntimeException();
170 else if (aPropertyName.compareToAscii(SC_UNO_TABSTOPDIS) == 0)
172 ScDocument* pDoc = pDocShell->GetDocument();
173 if (pDoc)
175 ScDocOptions aDocOpt(pDoc->GetDocOptions());
176 sal_Int32 nValue = 0;
177 if (aValue >>= nValue)
179 aDocOpt.SetTabDistance(static_cast<sal_uInt16>(HMMToTwips(nValue)));
180 pDoc->SetDocOptions(aDocOpt);
183 else
184 throw uno::RuntimeException();
187 else if ( pMap->nWID == ATTR_FONT_LANGUAGE ||
188 pMap->nWID == ATTR_CJK_FONT_LANGUAGE ||
189 pMap->nWID == ATTR_CTL_FONT_LANGUAGE )
191 // for getPropertyValue the PoolDefaults are sufficient,
192 // but setPropertyValue has to be handled differently
194 lang::Locale aLocale;
195 if ( aValue >>= aLocale )
197 LanguageType eNew;
198 if (aLocale.Language.getLength() || aLocale.Country.getLength())
199 eNew = MsLangId::convertLocaleToLanguage( aLocale );
200 else
201 eNew = LANGUAGE_NONE;
203 ScDocument* pDoc = pDocShell->GetDocument();
204 LanguageType eLatin, eCjk, eCtl;
205 pDoc->GetLanguage( eLatin, eCjk, eCtl );
207 if ( pMap->nWID == ATTR_CJK_FONT_LANGUAGE )
208 eCjk = eNew;
209 else if ( pMap->nWID == ATTR_CTL_FONT_LANGUAGE )
210 eCtl = eNew;
211 else
212 eLatin = eNew;
214 pDoc->SetLanguage( eLatin, eCjk, eCtl );
217 else
219 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
220 SfxPoolItem* pNewItem = pPool->GetDefaultItem(pMap->nWID).Clone();
222 if( !pNewItem->PutValue( aValue, pMap->nMemberId ) )
223 throw lang::IllegalArgumentException();
225 pPool->SetPoolDefaultItem( *pNewItem );
226 delete pNewItem; // copied in SetPoolDefaultItem
228 ItemsChanged();
232 uno::Any SAL_CALL ScDocDefaultsObj::getPropertyValue( const rtl::OUString& aPropertyName )
233 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
234 uno::RuntimeException)
236 // use pool default if set
238 ScUnoGuard aGuard;
240 if ( !pDocShell )
241 throw uno::RuntimeException();
243 uno::Any aRet;
244 const SfxItemPropertyMap* pMap =
245 SfxItemPropertyMap::GetByName( lcl_GetDocDefaultsMap(), aPropertyName );
246 if ( !pMap )
247 throw beans::UnknownPropertyException();
249 if (!pMap->nWID)
251 if(aPropertyName.compareToAscii(SC_UNO_STANDARDDEC) == 0)
253 ScDocument* pDoc = pDocShell->GetDocument();
254 if (pDoc)
256 const ScDocOptions& aDocOpt = pDoc->GetDocOptions();
257 aRet <<= static_cast<sal_Int16> (aDocOpt.GetStdPrecision());
259 else
260 throw uno::RuntimeException();
262 else if (aPropertyName.compareToAscii(SC_UNO_TABSTOPDIS) == 0)
264 ScDocument* pDoc = pDocShell->GetDocument();
265 if (pDoc)
267 const ScDocOptions& aDocOpt = pDoc->GetDocOptions();
268 sal_Int32 nValue (TwipsToEvenHMM(aDocOpt.GetTabDistance()));
269 aRet <<= nValue;
271 else
272 throw uno::RuntimeException();
275 else
277 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
278 const SfxPoolItem& rItem = pPool->GetDefaultItem( pMap->nWID );
279 rItem.QueryValue( aRet, pMap->nMemberId );
281 return aRet;
284 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScDocDefaultsObj )
286 // XPropertyState
288 beans::PropertyState SAL_CALL ScDocDefaultsObj::getPropertyState( const rtl::OUString& aPropertyName )
289 throw(beans::UnknownPropertyException, uno::RuntimeException)
291 ScUnoGuard aGuard;
293 if ( !pDocShell )
294 throw uno::RuntimeException();
296 const SfxItemPropertyMap* pMap =
297 SfxItemPropertyMap::GetByName( lcl_GetDocDefaultsMap(), aPropertyName );
298 if ( !pMap )
299 throw beans::UnknownPropertyException();
301 beans::PropertyState eRet = beans::PropertyState_DEFAULT_VALUE;
303 USHORT nWID = pMap->nWID;
304 if ( nWID == ATTR_FONT || nWID == ATTR_CJK_FONT || nWID == ATTR_CTL_FONT || !nWID )
306 // static default for font is system-dependent,
307 // so font default is always treated as "direct value".
309 eRet = beans::PropertyState_DIRECT_VALUE;
311 else
313 // check if pool default is set
315 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
316 if ( pPool->GetPoolDefaultItem( nWID ) != NULL )
317 eRet = beans::PropertyState_DIRECT_VALUE;
320 return eRet;
323 uno::Sequence<beans::PropertyState> SAL_CALL ScDocDefaultsObj::getPropertyStates(
324 const uno::Sequence<rtl::OUString>& aPropertyNames )
325 throw(beans::UnknownPropertyException, uno::RuntimeException)
327 // the simple way: call getPropertyState
329 ScUnoGuard aGuard;
330 const rtl::OUString* pNames = aPropertyNames.getConstArray();
331 uno::Sequence<beans::PropertyState> aRet(aPropertyNames.getLength());
332 beans::PropertyState* pStates = aRet.getArray();
333 for(sal_Int32 i = 0; i < aPropertyNames.getLength(); i++)
334 pStates[i] = getPropertyState(pNames[i]);
335 return aRet;
338 void SAL_CALL ScDocDefaultsObj::setPropertyToDefault( const rtl::OUString& aPropertyName )
339 throw(beans::UnknownPropertyException, uno::RuntimeException)
341 ScUnoGuard aGuard;
343 if ( !pDocShell )
344 throw uno::RuntimeException();
346 const SfxItemPropertyMap* pMap =
347 SfxItemPropertyMap::GetByName( lcl_GetDocDefaultsMap(), aPropertyName );
348 if ( !pMap )
349 throw beans::UnknownPropertyException();
351 if (pMap->nWID)
353 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
354 pPool->ResetPoolDefaultItem( pMap->nWID );
356 ItemsChanged();
360 uno::Any SAL_CALL ScDocDefaultsObj::getPropertyDefault( const rtl::OUString& aPropertyName )
361 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
362 uno::RuntimeException)
364 // always use static default
366 ScUnoGuard aGuard;
368 if ( !pDocShell )
369 throw uno::RuntimeException();
371 const SfxItemPropertyMap* pMap =
372 SfxItemPropertyMap::GetByName( lcl_GetDocDefaultsMap(), aPropertyName );
373 if ( !pMap )
374 throw beans::UnknownPropertyException();
376 uno::Any aRet;
377 if (pMap->nWID)
379 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
380 const SfxPoolItem* pItem = pPool->GetItem( pMap->nWID, SFX_ITEMS_STATICDEFAULT );
381 if (pItem)
382 pItem->QueryValue( aRet, pMap->nMemberId );
384 return aRet;