merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / unoobj / defltuno.cxx
blobf91bb8194137514eac0e565dcd854852a2efff37
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 SfxItemPropertyMapEntry* lcl_GetDocDefaultsMap()
58 static SfxItemPropertyMapEntry 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 ),
97 aPropertyMap(lcl_GetDocDefaultsMap())
99 pDocShell->GetDocument()->AddUnoObject(*this);
102 ScDocDefaultsObj::~ScDocDefaultsObj()
104 if (pDocShell)
105 pDocShell->GetDocument()->RemoveUnoObject(*this);
108 void ScDocDefaultsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
110 if ( rHint.ISA( SfxSimpleHint ) &&
111 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
113 pDocShell = NULL; // document gone
117 void ScDocDefaultsObj::ItemsChanged()
119 if (pDocShell)
121 //! if not in XML import, adjust row heights
123 pDocShell->PostPaint( 0,0,0, MAXCOL,MAXROW,MAXTAB, PAINT_GRID );
127 // XPropertySet
129 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScDocDefaultsObj::getPropertySetInfo()
130 throw(uno::RuntimeException)
132 ScUnoGuard aGuard;
133 static uno::Reference<beans::XPropertySetInfo> aRef = new SfxItemPropertySetInfo(
134 &aPropertyMap );
135 return aRef;
138 void SAL_CALL ScDocDefaultsObj::setPropertyValue(
139 const rtl::OUString& aPropertyName, const uno::Any& aValue )
140 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
141 lang::IllegalArgumentException, lang::WrappedTargetException,
142 uno::RuntimeException)
144 ScUnoGuard aGuard;
146 if ( !pDocShell )
147 throw uno::RuntimeException();
149 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
150 if ( !pEntry )
151 throw beans::UnknownPropertyException();
152 if(!pEntry->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 ( pEntry->nWID == ATTR_FONT_LANGUAGE ||
188 pEntry->nWID == ATTR_CJK_FONT_LANGUAGE ||
189 pEntry->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 ( pEntry->nWID == ATTR_CJK_FONT_LANGUAGE )
208 eCjk = eNew;
209 else if ( pEntry->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(pEntry->nWID).Clone();
222 if( !pNewItem->PutValue( aValue, pEntry->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 SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
245 if ( !pEntry )
246 throw beans::UnknownPropertyException();
248 if (!pEntry->nWID)
250 if(aPropertyName.compareToAscii(SC_UNO_STANDARDDEC) == 0)
252 ScDocument* pDoc = pDocShell->GetDocument();
253 if (pDoc)
255 const ScDocOptions& aDocOpt = pDoc->GetDocOptions();
256 aRet <<= static_cast<sal_Int16> (aDocOpt.GetStdPrecision());
258 else
259 throw uno::RuntimeException();
261 else if (aPropertyName.compareToAscii(SC_UNO_TABSTOPDIS) == 0)
263 ScDocument* pDoc = pDocShell->GetDocument();
264 if (pDoc)
266 const ScDocOptions& aDocOpt = pDoc->GetDocOptions();
267 sal_Int32 nValue (TwipsToEvenHMM(aDocOpt.GetTabDistance()));
268 aRet <<= nValue;
270 else
271 throw uno::RuntimeException();
274 else
276 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
277 const SfxPoolItem& rItem = pPool->GetDefaultItem( pEntry->nWID );
278 rItem.QueryValue( aRet, pEntry->nMemberId );
280 return aRet;
283 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScDocDefaultsObj )
285 // XPropertyState
287 beans::PropertyState SAL_CALL ScDocDefaultsObj::getPropertyState( const rtl::OUString& aPropertyName )
288 throw(beans::UnknownPropertyException, uno::RuntimeException)
290 ScUnoGuard aGuard;
292 if ( !pDocShell )
293 throw uno::RuntimeException();
295 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
296 if ( !pEntry )
297 throw beans::UnknownPropertyException();
299 beans::PropertyState eRet = beans::PropertyState_DEFAULT_VALUE;
301 USHORT nWID = pEntry->nWID;
302 if ( nWID == ATTR_FONT || nWID == ATTR_CJK_FONT || nWID == ATTR_CTL_FONT || !nWID )
304 // static default for font is system-dependent,
305 // so font default is always treated as "direct value".
307 eRet = beans::PropertyState_DIRECT_VALUE;
309 else
311 // check if pool default is set
313 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
314 if ( pPool->GetPoolDefaultItem( nWID ) != NULL )
315 eRet = beans::PropertyState_DIRECT_VALUE;
318 return eRet;
321 uno::Sequence<beans::PropertyState> SAL_CALL ScDocDefaultsObj::getPropertyStates(
322 const uno::Sequence<rtl::OUString>& aPropertyNames )
323 throw(beans::UnknownPropertyException, uno::RuntimeException)
325 // the simple way: call getPropertyState
327 ScUnoGuard aGuard;
328 const rtl::OUString* pNames = aPropertyNames.getConstArray();
329 uno::Sequence<beans::PropertyState> aRet(aPropertyNames.getLength());
330 beans::PropertyState* pStates = aRet.getArray();
331 for(sal_Int32 i = 0; i < aPropertyNames.getLength(); i++)
332 pStates[i] = getPropertyState(pNames[i]);
333 return aRet;
336 void SAL_CALL ScDocDefaultsObj::setPropertyToDefault( const rtl::OUString& aPropertyName )
337 throw(beans::UnknownPropertyException, uno::RuntimeException)
339 ScUnoGuard aGuard;
341 if ( !pDocShell )
342 throw uno::RuntimeException();
344 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
345 if ( !pEntry )
346 throw beans::UnknownPropertyException();
348 if (pEntry->nWID)
350 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
351 pPool->ResetPoolDefaultItem( pEntry->nWID );
353 ItemsChanged();
357 uno::Any SAL_CALL ScDocDefaultsObj::getPropertyDefault( const rtl::OUString& aPropertyName )
358 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
359 uno::RuntimeException)
361 // always use static default
363 ScUnoGuard aGuard;
365 if ( !pDocShell )
366 throw uno::RuntimeException();
368 const SfxItemPropertySimpleEntry* pEntry = aPropertyMap.getByName( aPropertyName );
369 if ( !pEntry )
370 throw beans::UnknownPropertyException();
372 uno::Any aRet;
373 if (pEntry->nWID)
375 ScDocumentPool* pPool = pDocShell->GetDocument()->GetPool();
376 const SfxPoolItem* pItem = pPool->GetItem( pEntry->nWID, SFX_ITEMS_STATICDEFAULT );
377 if (pItem)
378 pItem->QueryValue( aRet, pEntry->nMemberId );
380 return aRet;