Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/table/CellAddress.hpp>
21 : #include <com/sun/star/table/CellRangeAddress.hpp>
22 : #include <cppuhelper/supportsservice.hxx>
23 :
24 : #include <svl/itemprop.hxx>
25 : #include <vcl/svapp.hxx>
26 :
27 : #include "docsh.hxx"
28 : #include "unonames.hxx"
29 : #include "miscuno.hxx"
30 : #include "convuno.hxx"
31 : #include "addruno.hxx"
32 :
33 : using namespace com::sun::star;
34 :
35 16 : ScAddressConversionObj::ScAddressConversionObj(ScDocShell* pDocSh, bool _bIsRange) :
36 : pDocShell( pDocSh ),
37 : nRefSheet( 0 ),
38 16 : bIsRange( _bIsRange )
39 : {
40 16 : pDocShell->GetDocument().AddUnoObject(*this);
41 16 : }
42 :
43 48 : ScAddressConversionObj::~ScAddressConversionObj()
44 : {
45 16 : SolarMutexGuard g;
46 :
47 16 : if (pDocShell)
48 12 : pDocShell->GetDocument().RemoveUnoObject(*this);
49 32 : }
50 :
51 284 : void ScAddressConversionObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
52 : {
53 284 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
54 284 : if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
55 : {
56 4 : pDocShell = NULL; // invalid
57 : }
58 284 : }
59 :
60 4 : bool ScAddressConversionObj::ParseUIString( const OUString& rUIString, ::formula::FormulaGrammar::AddressConvention eConv )
61 : {
62 4 : if (!pDocShell)
63 0 : return false;
64 :
65 4 : ScDocument& rDoc = pDocShell->GetDocument();
66 4 : bool bSuccess = false;
67 4 : if ( bIsRange )
68 : {
69 4 : sal_uInt16 nResult = aRange.ParseAny( rUIString, &rDoc, eConv );
70 4 : if ( nResult & SCA_VALID )
71 : {
72 4 : if ( ( nResult & SCA_TAB_3D ) == 0 )
73 2 : aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
74 4 : if ( ( nResult & SCA_TAB2_3D ) == 0 )
75 2 : aRange.aEnd.SetTab( aRange.aStart.Tab() );
76 : // different sheets are not supported in CellRangeAddress
77 4 : if ( aRange.aStart.Tab() == aRange.aEnd.Tab() )
78 4 : bSuccess = true;
79 : }
80 : }
81 : else
82 : {
83 0 : sal_uInt16 nResult = aRange.aStart.Parse( rUIString, &rDoc, eConv );
84 0 : if ( nResult & SCA_VALID )
85 : {
86 0 : if ( ( nResult & SCA_TAB_3D ) == 0 )
87 0 : aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
88 0 : bSuccess = true;
89 : }
90 : }
91 4 : return bSuccess;
92 : }
93 :
94 : // XPropertySet
95 :
96 2 : uno::Reference<beans::XPropertySetInfo> SAL_CALL ScAddressConversionObj::getPropertySetInfo()
97 : throw(uno::RuntimeException, std::exception)
98 : {
99 2 : SolarMutexGuard aGuard;
100 :
101 2 : if ( bIsRange )
102 : {
103 : static const SfxItemPropertyMapEntry aPropertyMap[] =
104 : {
105 2 : { OUString(SC_UNONAME_ADDRESS), 0, cppu::UnoType<table::CellRangeAddress>::get(), 0, 0 },
106 2 : { OUString(SC_UNONAME_PERSREPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
107 2 : { OUString(SC_UNONAME_XLA1REPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
108 2 : { OUString(SC_UNONAME_REFSHEET), 0, cppu::UnoType<sal_Int32>::get(), 0, 0 },
109 2 : { OUString(SC_UNONAME_UIREPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
110 2 : { OUString(SC_UNONAME_XLA1REPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
111 : { OUString(), 0, css::uno::Type(), 0, 0 }
112 16 : };
113 2 : static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
114 2 : return aRef;
115 : }
116 : else
117 : {
118 : static const SfxItemPropertyMapEntry aPropertyMap[] =
119 : {
120 0 : { OUString(SC_UNONAME_ADDRESS), 0, cppu::UnoType<table::CellAddress>::get(), 0, 0 },
121 0 : { OUString(SC_UNONAME_PERSREPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
122 0 : { OUString(SC_UNONAME_XLA1REPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
123 0 : { OUString(SC_UNONAME_REFSHEET), 0, cppu::UnoType<sal_Int32>::get(), 0, 0 },
124 0 : { OUString(SC_UNONAME_UIREPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
125 0 : { OUString(SC_UNONAME_XLA1REPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
126 : { OUString(), 0, css::uno::Type(), 0, 0 }
127 0 : };
128 0 : static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
129 0 : return aRef;
130 2 : }
131 : }
132 :
133 8 : void SAL_CALL ScAddressConversionObj::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
134 : throw(beans::UnknownPropertyException, beans::PropertyVetoException,
135 : lang::IllegalArgumentException, lang::WrappedTargetException,
136 : uno::RuntimeException, std::exception)
137 : {
138 8 : if ( !pDocShell )
139 0 : throw uno::RuntimeException();
140 :
141 8 : bool bSuccess = false;
142 8 : OUString aNameStr(aPropertyName);
143 8 : if ( aNameStr.equalsAscii( SC_UNONAME_ADDRESS ) )
144 : {
145 : // read the cell/range address from API struct
146 2 : if ( bIsRange )
147 : {
148 2 : table::CellRangeAddress aRangeAddress;
149 2 : if ( aValue >>= aRangeAddress )
150 : {
151 2 : ScUnoConversion::FillScRange( aRange, aRangeAddress );
152 2 : bSuccess = true;
153 : }
154 : }
155 : else
156 : {
157 0 : table::CellAddress aCellAddress;
158 0 : if ( aValue >>= aCellAddress )
159 : {
160 0 : ScUnoConversion::FillScAddress( aRange.aStart, aCellAddress );
161 0 : bSuccess = true;
162 : }
163 : }
164 : }
165 6 : else if ( aNameStr.equalsAscii( SC_UNONAME_REFSHEET ) )
166 : {
167 : // set the reference sheet
168 2 : sal_Int32 nIntVal = 0;
169 2 : if ( aValue >>= nIntVal )
170 : {
171 2 : nRefSheet = nIntVal;
172 2 : bSuccess = true;
173 : }
174 : }
175 4 : else if ( aNameStr.equalsAscii( SC_UNONAME_UIREPR ) )
176 : {
177 : // parse the UI representation string
178 0 : OUString sRepresentation;
179 0 : if (aValue >>= sRepresentation)
180 : {
181 0 : OUString aUIString = sRepresentation;
182 0 : bSuccess = ParseUIString( aUIString );
183 0 : }
184 : }
185 4 : else if ( aNameStr.equalsAscii( SC_UNONAME_PERSREPR ) || aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) )
186 : {
187 4 : ::formula::FormulaGrammar::AddressConvention eConv = aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) ?
188 4 : ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
189 :
190 : // parse the file format string
191 4 : OUString sRepresentation;
192 4 : if (aValue >>= sRepresentation)
193 : {
194 4 : OUString aUIString(sRepresentation);
195 :
196 : // cell or range: strip a single "." at the start
197 4 : if ( aUIString[0]== (sal_Unicode) '.' )
198 0 : aUIString = aUIString.copy( 1 );
199 :
200 4 : if ( bIsRange )
201 : {
202 : // range: also strip a "." after the last colon
203 4 : sal_Int32 nColon = OUString(aUIString).lastIndexOf( (sal_Unicode) ':' );
204 8 : if ( nColon >= 0 && nColon < aUIString.getLength() - 1 &&
205 4 : aUIString[nColon+1] == '.' )
206 0 : aUIString = aUIString.replaceAt( nColon+1, 1, "" );
207 : }
208 :
209 : // parse the rest like a UI string
210 4 : bSuccess = ParseUIString( aUIString, eConv );
211 4 : }
212 : }
213 : else
214 0 : throw beans::UnknownPropertyException();
215 :
216 8 : if ( !bSuccess )
217 0 : throw lang::IllegalArgumentException();
218 8 : }
219 :
220 6 : uno::Any SAL_CALL ScAddressConversionObj::getPropertyValue( const OUString& aPropertyName )
221 : throw(beans::UnknownPropertyException, lang::WrappedTargetException,
222 : uno::RuntimeException, std::exception)
223 : {
224 6 : if ( !pDocShell )
225 0 : throw uno::RuntimeException();
226 :
227 6 : ScDocument& rDoc = pDocShell->GetDocument();
228 6 : uno::Any aRet;
229 :
230 12 : OUString aNameStr(aPropertyName);
231 6 : if ( aNameStr.equalsAscii( SC_UNONAME_ADDRESS ) )
232 : {
233 4 : if ( bIsRange )
234 : {
235 4 : table::CellRangeAddress aRangeAddress;
236 4 : ScUnoConversion::FillApiRange( aRangeAddress, aRange );
237 4 : aRet <<= aRangeAddress;
238 : }
239 : else
240 : {
241 0 : table::CellAddress aCellAddress;
242 0 : ScUnoConversion::FillApiAddress( aCellAddress, aRange.aStart );
243 0 : aRet <<= aCellAddress;
244 : }
245 : }
246 2 : else if ( aNameStr.equalsAscii( SC_UNONAME_REFSHEET ) )
247 : {
248 0 : aRet <<= nRefSheet;
249 : }
250 2 : else if ( aNameStr.equalsAscii( SC_UNONAME_UIREPR ) )
251 : {
252 : // generate UI representation string - include sheet only if different from ref sheet
253 0 : OUString aFormatStr;
254 0 : sal_uInt16 nFlags = SCA_VALID;
255 0 : if ( aRange.aStart.Tab() != nRefSheet )
256 0 : nFlags |= SCA_TAB_3D;
257 0 : if ( bIsRange )
258 0 : aFormatStr = aRange.Format(nFlags, &rDoc);
259 : else
260 0 : aFormatStr = aRange.aStart.Format(nFlags, &rDoc);
261 0 : aRet <<= aFormatStr;
262 : }
263 2 : else if ( aNameStr.equalsAscii( SC_UNONAME_PERSREPR ) || aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) )
264 : {
265 2 : ::formula::FormulaGrammar::AddressConvention eConv = aNameStr.equalsAscii( SC_UNONAME_XLA1REPR ) ?
266 2 : ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
267 :
268 : // generate file format string - always include sheet
269 2 : OUString aFormatStr(aRange.aStart.Format(SCA_VALID | SCA_TAB_3D, &rDoc, eConv));
270 2 : if ( bIsRange )
271 : {
272 : // manually concatenate range so both parts always have the sheet name
273 2 : aFormatStr += ":";
274 2 : sal_uInt16 nFlags = SCA_VALID;
275 2 : if( eConv != ::formula::FormulaGrammar::CONV_XL_A1 )
276 2 : nFlags |= SCA_TAB_3D;
277 2 : OUString aSecond(aRange.aEnd.Format(nFlags, &rDoc, eConv));
278 2 : aFormatStr += aSecond ;
279 : }
280 2 : aRet <<= OUString( aFormatStr );
281 : }
282 : else
283 0 : throw beans::UnknownPropertyException();
284 :
285 12 : return aRet;
286 : }
287 :
288 0 : SC_IMPL_DUMMY_PROPERTY_LISTENER( ScAddressConversionObj )
289 :
290 : // lang::XServiceInfo
291 :
292 0 : OUString SAL_CALL ScAddressConversionObj::getImplementationName() throw(uno::RuntimeException, std::exception)
293 : {
294 0 : return OUString("ScAddressConversionObj" );
295 : }
296 :
297 0 : sal_Bool SAL_CALL ScAddressConversionObj::supportsService( const OUString& rServiceName )
298 : throw(uno::RuntimeException, std::exception)
299 : {
300 0 : return cppu::supportsService(this, rServiceName);
301 : }
302 :
303 0 : uno::Sequence<OUString> SAL_CALL ScAddressConversionObj::getSupportedServiceNames()
304 : throw(uno::RuntimeException, std::exception)
305 : {
306 0 : uno::Sequence<OUString> aRet(1);
307 0 : OUString* pArray = aRet.getArray();
308 0 : pArray[0] = bIsRange ? OUString(SC_SERVICENAME_RANGEADDRESS)
309 0 : : OUString(SC_SERVICENAME_CELLADDRESS);
310 0 : return aRet;
311 228 : }
312 :
313 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|