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 <com/sun/star/table/CellAddress.hpp>
21 #include <com/sun/star/table/CellRangeAddress.hpp>
23 #include <svl/itemprop.hxx>
24 #include <vcl/svapp.hxx>
27 #include "unonames.hxx"
28 #include "miscuno.hxx"
29 #include "convuno.hxx"
30 #include "addruno.hxx"
32 using namespace com::sun::star
;
34 //------------------------------------------------------------------------
36 ScAddressConversionObj::ScAddressConversionObj(ScDocShell
* pDocSh
, sal_Bool bForRange
) :
41 pDocShell
->GetDocument()->AddUnoObject(*this);
44 ScAddressConversionObj::~ScAddressConversionObj()
47 pDocShell
->GetDocument()->RemoveUnoObject(*this);
50 void ScAddressConversionObj::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
52 if ( rHint
.ISA( SfxSimpleHint
) &&
53 ((const SfxSimpleHint
&)rHint
).GetId() == SFX_HINT_DYING
)
55 pDocShell
= NULL
; // invalid
59 sal_Bool
ScAddressConversionObj::ParseUIString( const OUString
& rUIString
, ::formula::FormulaGrammar::AddressConvention eConv
)
64 ScDocument
* pDoc
= pDocShell
->GetDocument();
65 sal_Bool bSuccess
= false;
68 sal_uInt16 nResult
= aRange
.ParseAny( rUIString
, pDoc
, eConv
);
69 if ( nResult
& SCA_VALID
)
71 if ( ( nResult
& SCA_TAB_3D
) == 0 )
72 aRange
.aStart
.SetTab( static_cast<SCTAB
>(nRefSheet
) );
73 if ( ( nResult
& SCA_TAB2_3D
) == 0 )
74 aRange
.aEnd
.SetTab( aRange
.aStart
.Tab() );
75 // different sheets are not supported in CellRangeAddress
76 if ( aRange
.aStart
.Tab() == aRange
.aEnd
.Tab() )
82 sal_uInt16 nResult
= aRange
.aStart
.Parse( rUIString
, pDoc
, eConv
);
83 if ( nResult
& SCA_VALID
)
85 if ( ( nResult
& SCA_TAB_3D
) == 0 )
86 aRange
.aStart
.SetTab( static_cast<SCTAB
>(nRefSheet
) );
95 uno::Reference
<beans::XPropertySetInfo
> SAL_CALL
ScAddressConversionObj::getPropertySetInfo()
96 throw(uno::RuntimeException
)
98 SolarMutexGuard aGuard
;
102 static const SfxItemPropertyMapEntry aPropertyMap
[] =
104 {MAP_CHAR_LEN(SC_UNONAME_ADDRESS
), 0, &getCppuType((table::CellRangeAddress
*)0), 0, 0 },
105 {MAP_CHAR_LEN(SC_UNONAME_PERSREPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
106 {MAP_CHAR_LEN(SC_UNONAME_XLA1REPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
107 {MAP_CHAR_LEN(SC_UNONAME_REFSHEET
), 0, &getCppuType((sal_Int32
*)0), 0, 0 },
108 {MAP_CHAR_LEN(SC_UNONAME_UIREPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
109 {MAP_CHAR_LEN(SC_UNONAME_XLA1REPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
112 static uno::Reference
<beans::XPropertySetInfo
> aRef(new SfxItemPropertySetInfo( aPropertyMap
));
117 static const SfxItemPropertyMapEntry aPropertyMap
[] =
119 {MAP_CHAR_LEN(SC_UNONAME_ADDRESS
), 0, &getCppuType((table::CellAddress
*)0), 0, 0 },
120 {MAP_CHAR_LEN(SC_UNONAME_PERSREPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
121 {MAP_CHAR_LEN(SC_UNONAME_XLA1REPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
122 {MAP_CHAR_LEN(SC_UNONAME_REFSHEET
), 0, &getCppuType((sal_Int32
*)0), 0, 0 },
123 {MAP_CHAR_LEN(SC_UNONAME_UIREPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
124 {MAP_CHAR_LEN(SC_UNONAME_XLA1REPR
), 0, &getCppuType((OUString
*)0), 0, 0 },
127 static uno::Reference
<beans::XPropertySetInfo
> aRef(new SfxItemPropertySetInfo( aPropertyMap
));
132 void SAL_CALL
ScAddressConversionObj::setPropertyValue( const OUString
& aPropertyName
, const uno::Any
& aValue
)
133 throw(beans::UnknownPropertyException
, beans::PropertyVetoException
,
134 lang::IllegalArgumentException
, lang::WrappedTargetException
,
135 uno::RuntimeException
)
138 throw uno::RuntimeException();
140 sal_Bool bSuccess
= false;
141 OUString
aNameStr(aPropertyName
);
142 if ( aNameStr
.equalsAscii( SC_UNONAME_ADDRESS
) )
144 // read the cell/range address from API struct
147 table::CellRangeAddress aRangeAddress
;
148 if ( aValue
>>= aRangeAddress
)
150 ScUnoConversion::FillScRange( aRange
, aRangeAddress
);
156 table::CellAddress aCellAddress
;
157 if ( aValue
>>= aCellAddress
)
159 ScUnoConversion::FillScAddress( aRange
.aStart
, aCellAddress
);
164 else if ( aNameStr
.equalsAscii( SC_UNONAME_REFSHEET
) )
166 // set the reference sheet
167 sal_Int32 nIntVal
= 0;
168 if ( aValue
>>= nIntVal
)
174 else if ( aNameStr
.equalsAscii( SC_UNONAME_UIREPR
) )
176 // parse the UI representation string
177 OUString sRepresentation
;
178 if (aValue
>>= sRepresentation
)
180 OUString aUIString
= sRepresentation
;
181 bSuccess
= ParseUIString( aUIString
);
184 else if ( aNameStr
.equalsAscii( SC_UNONAME_PERSREPR
) || aNameStr
.equalsAscii( SC_UNONAME_XLA1REPR
) )
186 ::formula::FormulaGrammar::AddressConvention eConv
= aNameStr
.equalsAscii( SC_UNONAME_XLA1REPR
) ?
187 ::formula::FormulaGrammar::CONV_XL_A1
: ::formula::FormulaGrammar::CONV_OOO
;
189 // parse the file format string
190 OUString sRepresentation
;
191 if (aValue
>>= sRepresentation
)
193 OUString
aUIString(sRepresentation
);
195 // cell or range: strip a single "." at the start
196 if ( aUIString
[0]== (sal_Unicode
) '.' )
197 aUIString
= aUIString
.copy( 1 );
201 // range: also strip a "." after the last colon
202 sal_Int32 nColon
= OUString(aUIString
).lastIndexOf( (sal_Unicode
) ':' );
203 if ( nColon
>= 0 && nColon
< aUIString
.getLength() - 1 &&
204 aUIString
[nColon
+1] == '.' )
205 aUIString
= aUIString
.replaceAt( nColon
+1, 1, "" );
208 // parse the rest like a UI string
209 bSuccess
= ParseUIString( aUIString
, eConv
);
213 throw beans::UnknownPropertyException();
216 throw lang::IllegalArgumentException();
219 uno::Any SAL_CALL
ScAddressConversionObj::getPropertyValue( const OUString
& aPropertyName
)
220 throw(beans::UnknownPropertyException
, lang::WrappedTargetException
,
221 uno::RuntimeException
)
224 throw uno::RuntimeException();
226 ScDocument
* pDoc
= pDocShell
->GetDocument();
229 OUString
aNameStr(aPropertyName
);
230 if ( aNameStr
.equalsAscii( SC_UNONAME_ADDRESS
) )
234 table::CellRangeAddress aRangeAddress
;
235 ScUnoConversion::FillApiRange( aRangeAddress
, aRange
);
236 aRet
<<= aRangeAddress
;
240 table::CellAddress aCellAddress
;
241 ScUnoConversion::FillApiAddress( aCellAddress
, aRange
.aStart
);
242 aRet
<<= aCellAddress
;
245 else if ( aNameStr
.equalsAscii( SC_UNONAME_REFSHEET
) )
249 else if ( aNameStr
.equalsAscii( SC_UNONAME_UIREPR
) )
251 // generate UI representation string - include sheet only if different from ref sheet
253 sal_uInt16 nFlags
= SCA_VALID
;
254 if ( aRange
.aStart
.Tab() != nRefSheet
)
255 nFlags
|= SCA_TAB_3D
;
257 aFormatStr
= aRange
.Format(nFlags
, pDoc
);
259 aFormatStr
= aRange
.aStart
.Format(nFlags
, pDoc
);
262 else if ( aNameStr
.equalsAscii( SC_UNONAME_PERSREPR
) || aNameStr
.equalsAscii( SC_UNONAME_XLA1REPR
) )
264 ::formula::FormulaGrammar::AddressConvention eConv
= aNameStr
.equalsAscii( SC_UNONAME_XLA1REPR
) ?
265 ::formula::FormulaGrammar::CONV_XL_A1
: ::formula::FormulaGrammar::CONV_OOO
;
267 // generate file format string - always include sheet
268 OUString
aFormatStr(aRange
.aStart
.Format(SCA_VALID
| SCA_TAB_3D
, pDoc
, eConv
));
271 // manually concatenate range so both parts always have the sheet name
273 sal_uInt16 nFlags
= SCA_VALID
;
274 if( eConv
!= ::formula::FormulaGrammar::CONV_XL_A1
)
275 nFlags
|= SCA_TAB_3D
;
276 OUString
aSecond(aRange
.aEnd
.Format(nFlags
, pDoc
, eConv
));
277 aFormatStr
+= aSecond
;
279 aRet
<<= OUString( aFormatStr
);
282 throw beans::UnknownPropertyException();
287 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScAddressConversionObj
)
289 // lang::XServiceInfo
291 OUString SAL_CALL
ScAddressConversionObj::getImplementationName() throw(uno::RuntimeException
)
293 return OUString("ScAddressConversionObj" );
296 sal_Bool SAL_CALL
ScAddressConversionObj::supportsService( const OUString
& rServiceName
)
297 throw(uno::RuntimeException
)
299 OUString
aServiceStr( rServiceName
);
300 return aServiceStr
.equalsAscii( bIsRange
? SC_SERVICENAME_RANGEADDRESS
301 : SC_SERVICENAME_CELLADDRESS
);
304 uno::Sequence
<OUString
> SAL_CALL
ScAddressConversionObj::getSupportedServiceNames()
305 throw(uno::RuntimeException
)
307 uno::Sequence
<OUString
> aRet(1);
308 OUString
* pArray
= aRet
.getArray();
309 pArray
[0] = bIsRange
? OUString(SC_SERVICENAME_RANGEADDRESS
)
310 : OUString(SC_SERVICENAME_CELLADDRESS
);
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */