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