sync master with lastest vba changes
[ooovba.git] / sc / source / ui / unoobj / addruno.cxx
blob9ea96b41e767562352501517d375194be8b0e1fb
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: addruno.cxx,v $
10 * $Revision: 1.9 $
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 <com/sun/star/table/CellAddress.hpp>
37 #include <com/sun/star/table/CellRangeAddress.hpp>
39 #include <svtools/itemprop.hxx>
41 #include "docsh.hxx"
42 #include "unonames.hxx"
43 #include "unoguard.hxx"
44 #include "miscuno.hxx"
45 #include "convuno.hxx"
46 #include "addruno.hxx"
48 using namespace com::sun::star;
50 //------------------------------------------------------------------------
52 ScAddressConversionObj::ScAddressConversionObj(ScDocShell* pDocSh, sal_Bool bForRange) :
53 pDocShell( pDocSh ),
54 nRefSheet( 0 ),
55 bIsRange( bForRange )
57 pDocShell->GetDocument()->AddUnoObject(*this);
60 ScAddressConversionObj::~ScAddressConversionObj()
62 if (pDocShell)
63 pDocShell->GetDocument()->RemoveUnoObject(*this);
66 void ScAddressConversionObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
68 if ( rHint.ISA( SfxSimpleHint ) &&
69 ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
71 pDocShell = NULL; // invalid
75 sal_Bool ScAddressConversionObj::ParseUIString( const String& rUIString, ::formula::FormulaGrammar::AddressConvention eConv )
77 if (!pDocShell)
78 return sal_False;
80 ScDocument* pDoc = pDocShell->GetDocument();
81 sal_Bool bSuccess = sal_False;
82 if ( bIsRange )
84 USHORT nResult = aRange.ParseAny( rUIString, pDoc, eConv );
85 if ( nResult & SCA_VALID )
87 if ( ( nResult & SCA_TAB_3D ) == 0 )
88 aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
89 if ( ( nResult & SCA_TAB2_3D ) == 0 )
90 aRange.aEnd.SetTab( aRange.aStart.Tab() );
91 // different sheets are not supported in CellRangeAddress
92 if ( aRange.aStart.Tab() == aRange.aEnd.Tab() )
93 bSuccess = sal_True;
96 else
98 USHORT nResult = aRange.aStart.Parse( rUIString, pDoc, eConv );
99 if ( nResult & SCA_VALID )
101 if ( ( nResult & SCA_TAB_3D ) == 0 )
102 aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
103 bSuccess = sal_True;
106 return bSuccess;
109 // XPropertySet
111 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScAddressConversionObj::getPropertySetInfo()
112 throw(uno::RuntimeException)
114 ScUnoGuard aGuard;
116 if ( bIsRange )
118 static SfxItemPropertyMap aPropertyMap[] =
120 {MAP_CHAR_LEN(SC_UNONAME_ADDRESS), 0, &getCppuType((table::CellRangeAddress*)0), 0, 0 },
121 {MAP_CHAR_LEN(SC_UNONAME_PERSREPR), 0, &getCppuType((rtl::OUString*)0), 0, 0 },
122 {MAP_CHAR_LEN(SC_UNONAME_XL_A1_REPR), 0, &getCppuType((rtl::OUString*)0), 0, 0 },
123 {MAP_CHAR_LEN(SC_UNONAME_REFSHEET), 0, &getCppuType((sal_Int32*)0), 0, 0 },
124 {MAP_CHAR_LEN(SC_UNONAME_UIREPR), 0, &getCppuType((rtl::OUString*)0), 0, 0 },
125 {0,0,0,0,0,0}
127 static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
128 return aRef;
130 else
132 static SfxItemPropertyMap aPropertyMap[] =
134 {MAP_CHAR_LEN(SC_UNONAME_ADDRESS), 0, &getCppuType((table::CellAddress*)0), 0, 0 },
135 {MAP_CHAR_LEN(SC_UNONAME_PERSREPR), 0, &getCppuType((rtl::OUString*)0), 0, 0 },
136 {MAP_CHAR_LEN(SC_UNONAME_XL_A1_REPR), 0, &getCppuType((rtl::OUString*)0), 0, 0 },
137 {MAP_CHAR_LEN(SC_UNONAME_REFSHEET), 0, &getCppuType((sal_Int32*)0), 0, 0 },
138 {MAP_CHAR_LEN(SC_UNONAME_UIREPR), 0, &getCppuType((rtl::OUString*)0), 0, 0 },
139 {0,0,0,0,0,0}
141 static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
142 return aRef;
146 void SAL_CALL ScAddressConversionObj::setPropertyValue( const rtl::OUString& aPropertyName, const uno::Any& aValue )
147 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
148 lang::IllegalArgumentException, lang::WrappedTargetException,
149 uno::RuntimeException)
151 if ( !pDocShell )
152 throw uno::RuntimeException();
154 sal_Bool bSuccess = sal_False;
155 String aNameStr(aPropertyName);
156 if ( aNameStr.EqualsAscii( SC_UNONAME_ADDRESS ) )
158 // read the cell/range address from API struct
159 if ( bIsRange )
161 table::CellRangeAddress aRangeAddress;
162 if ( aValue >>= aRangeAddress )
164 ScUnoConversion::FillScRange( aRange, aRangeAddress );
165 bSuccess = sal_True;
168 else
170 table::CellAddress aCellAddress;
171 if ( aValue >>= aCellAddress )
173 ScUnoConversion::FillScAddress( aRange.aStart, aCellAddress );
174 bSuccess = sal_True;
178 else if ( aNameStr.EqualsAscii( SC_UNONAME_REFSHEET ) )
180 // set the reference sheet
181 sal_Int32 nIntVal = 0;
182 if ( aValue >>= nIntVal )
184 nRefSheet = nIntVal;
185 bSuccess = sal_True;
188 else if ( aNameStr.EqualsAscii( SC_UNONAME_UIREPR ) )
190 // parse the UI representation string
191 rtl::OUString sRepresentation;
192 if (aValue >>= sRepresentation)
194 String aUIString = sRepresentation;
195 bSuccess = ParseUIString( aUIString );
198 else if ( aNameStr.EqualsAscii( SC_UNONAME_PERSREPR ) || aNameStr.EqualsAscii( SC_UNONAME_XL_A1_REPR ) )
200 ::formula::FormulaGrammar::AddressConvention aConv = ::formula::FormulaGrammar::CONV_OOO;
201 if ( aNameStr.EqualsAscii( SC_UNONAME_XL_A1_REPR ) )
202 aConv = ::formula::FormulaGrammar::CONV_XL_A1;
203 // parse the file format string
204 rtl::OUString sRepresentation;
205 if (aValue >>= sRepresentation)
207 String aUIString(sRepresentation);
209 // cell or range: strip a single "." at the start
210 if ( aUIString.GetChar(0) == (sal_Unicode) '.' )
211 aUIString.Erase( 0, 1 );
213 if ( bIsRange )
215 // range: also strip a "." after the last colon
216 sal_Int32 nColon = rtl::OUString(aUIString).lastIndexOf( (sal_Unicode) ':' );
217 if ( nColon >= 0 && nColon < aUIString.Len() - 1 &&
218 aUIString.GetChar((xub_StrLen)nColon+1) == (sal_Unicode) '.' )
219 aUIString.Erase( (xub_StrLen)nColon+1, 1 );
222 // parse the rest like a UI string
223 bSuccess = ParseUIString( aUIString, aConv );
226 else
227 throw beans::UnknownPropertyException();
229 if ( !bSuccess )
230 throw lang::IllegalArgumentException();
233 uno::Any SAL_CALL ScAddressConversionObj::getPropertyValue( const rtl::OUString& aPropertyName )
234 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
235 uno::RuntimeException)
237 if ( !pDocShell )
238 throw uno::RuntimeException();
240 ScDocument* pDoc = pDocShell->GetDocument();
241 uno::Any aRet;
243 String aNameStr(aPropertyName);
244 if ( aNameStr.EqualsAscii( SC_UNONAME_ADDRESS ) )
246 if ( bIsRange )
248 table::CellRangeAddress aRangeAddress;
249 ScUnoConversion::FillApiRange( aRangeAddress, aRange );
250 aRet <<= aRangeAddress;
252 else
254 table::CellAddress aCellAddress;
255 ScUnoConversion::FillApiAddress( aCellAddress, aRange.aStart );
256 aRet <<= aCellAddress;
259 else if ( aNameStr.EqualsAscii( SC_UNONAME_REFSHEET ) )
261 aRet <<= nRefSheet;
263 else if ( aNameStr.EqualsAscii( SC_UNONAME_UIREPR ) )
265 // generate UI representation string - include sheet only if different from ref sheet
266 String aFormatStr;
267 USHORT nFlags = SCA_VALID;
268 if ( aRange.aStart.Tab() != nRefSheet )
269 nFlags |= SCA_TAB_3D;
270 if ( bIsRange )
271 aRange.Format( aFormatStr, nFlags, pDoc );
272 else
273 aRange.aStart.Format( aFormatStr, nFlags, pDoc );
274 aRet <<= rtl::OUString( aFormatStr );
276 else if ( aNameStr.EqualsAscii( SC_UNONAME_PERSREPR ) || aNameStr.EqualsAscii( SC_UNONAME_XL_A1_REPR ) )
278 ::formula::FormulaGrammar::AddressConvention aConv = ::formula::FormulaGrammar::CONV_OOO;
279 if ( aNameStr.EqualsAscii( SC_UNONAME_XL_A1_REPR ) )
280 aConv = ::formula::FormulaGrammar::CONV_XL_A1;
282 // generate file format string - always include sheet
283 String aFormatStr;
284 aRange.aStart.Format( aFormatStr, SCA_VALID | SCA_TAB_3D, pDoc, aConv );
285 if ( bIsRange )
287 // manually concatenate range so both parts always have the sheet name
288 aFormatStr.Append( (sal_Unicode) ':' );
289 String aSecond;
290 USHORT nFlags = SCA_VALID;
291 if( aConv != ::formula::FormulaGrammar::CONV_XL_A1 )
292 nFlags |= SCA_TAB_3D;
293 aRange.aEnd.Format( aSecond, SCA_VALID | SCA_TAB_3D, pDoc, aConv );
294 aFormatStr.Append( aSecond );
296 aRet <<= rtl::OUString( aFormatStr );
298 else
299 throw beans::UnknownPropertyException();
301 return aRet;
304 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScAddressConversionObj )
306 // lang::XServiceInfo
308 rtl::OUString SAL_CALL ScAddressConversionObj::getImplementationName() throw(uno::RuntimeException)
310 return rtl::OUString::createFromAscii( "ScAddressConversionObj" );
313 sal_Bool SAL_CALL ScAddressConversionObj::supportsService( const rtl::OUString& rServiceName )
314 throw(uno::RuntimeException)
316 String aServiceStr( rServiceName );
317 return aServiceStr.EqualsAscii( bIsRange ? SC_SERVICENAME_RANGEADDRESS
318 : SC_SERVICENAME_CELLADDRESS );
321 uno::Sequence<rtl::OUString> SAL_CALL ScAddressConversionObj::getSupportedServiceNames()
322 throw(uno::RuntimeException)
324 uno::Sequence<rtl::OUString> aRet(1);
325 rtl::OUString* pArray = aRet.getArray();
326 pArray[0] = rtl::OUString::createFromAscii( bIsRange ? SC_SERVICENAME_RANGEADDRESS
327 : SC_SERVICENAME_CELLADDRESS );
328 return aRet;