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 8 : ScAddressConversionObj::ScAddressConversionObj(ScDocShell* pDocSh, bool _bIsRange) :
36 : pDocShell( pDocSh ),
37 : nRefSheet( 0 ),
38 8 : bIsRange( _bIsRange )
39 : {
40 8 : pDocShell->GetDocument().AddUnoObject(*this);
41 8 : }
42 :
43 24 : ScAddressConversionObj::~ScAddressConversionObj()
44 : {
45 8 : SolarMutexGuard g;
46 :
47 8 : if (pDocShell)
48 8 : pDocShell->GetDocument().RemoveUnoObject(*this);
49 16 : }
50 :
51 2 : void ScAddressConversionObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
52 : {
53 2 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
54 2 : if ( pSimpleHint && pSimpleHint->GetId() == SFX_HINT_DYING )
55 : {
56 0 : pDocShell = NULL; // invalid
57 : }
58 2 : }
59 :
60 2 : bool ScAddressConversionObj::ParseUIString( const OUString& rUIString, ::formula::FormulaGrammar::AddressConvention eConv )
61 : {
62 2 : if (!pDocShell)
63 0 : return false;
64 :
65 2 : ScDocument& rDoc = pDocShell->GetDocument();
66 2 : bool bSuccess = false;
67 2 : if ( bIsRange )
68 : {
69 2 : sal_uInt16 nResult = aRange.ParseAny( rUIString, &rDoc, eConv );
70 2 : if ( nResult & SCA_VALID )
71 : {
72 2 : if ( ( nResult & SCA_TAB_3D ) == 0 )
73 1 : aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
74 2 : if ( ( nResult & SCA_TAB2_3D ) == 0 )
75 1 : aRange.aEnd.SetTab( aRange.aStart.Tab() );
76 : // different sheets are not supported in CellRangeAddress
77 2 : if ( aRange.aStart.Tab() == aRange.aEnd.Tab() )
78 2 : 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 2 : return bSuccess;
92 : }
93 :
94 : // XPropertySet
95 :
96 1 : uno::Reference<beans::XPropertySetInfo> SAL_CALL ScAddressConversionObj::getPropertySetInfo()
97 : throw(uno::RuntimeException, std::exception)
98 : {
99 1 : SolarMutexGuard aGuard;
100 :
101 1 : if ( bIsRange )
102 : {
103 : static const SfxItemPropertyMapEntry aPropertyMap[] =
104 : {
105 1 : { OUString(SC_UNONAME_ADDRESS), 0, cppu::UnoType<table::CellRangeAddress>::get(), 0, 0 },
106 1 : { OUString(SC_UNONAME_PERSREPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
107 1 : { OUString(SC_UNONAME_XLA1REPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
108 1 : { OUString(SC_UNONAME_REFSHEET), 0, cppu::UnoType<sal_Int32>::get(), 0, 0 },
109 1 : { OUString(SC_UNONAME_UIREPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
110 1 : { OUString(SC_UNONAME_XLA1REPR), 0, cppu::UnoType<OUString>::get(), 0, 0 },
111 : { OUString(), 0, css::uno::Type(), 0, 0 }
112 8 : };
113 1 : static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
114 1 : 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 1 : }
131 : }
132 :
133 4 : 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 4 : if ( !pDocShell )
139 0 : throw uno::RuntimeException();
140 :
141 4 : bool bSuccess = false;
142 4 : OUString aNameStr(aPropertyName);
143 4 : if ( aNameStr == SC_UNONAME_ADDRESS )
144 : {
145 : // read the cell/range address from API struct
146 1 : if ( bIsRange )
147 : {
148 1 : table::CellRangeAddress aRangeAddress;
149 1 : if ( aValue >>= aRangeAddress )
150 : {
151 1 : ScUnoConversion::FillScRange( aRange, aRangeAddress );
152 1 : 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 3 : else if ( aNameStr == SC_UNONAME_REFSHEET )
166 : {
167 : // set the reference sheet
168 1 : sal_Int32 nIntVal = 0;
169 1 : if ( aValue >>= nIntVal )
170 : {
171 1 : nRefSheet = nIntVal;
172 1 : bSuccess = true;
173 : }
174 : }
175 2 : else if ( aNameStr == 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 2 : else if ( aNameStr == SC_UNONAME_PERSREPR || aNameStr == SC_UNONAME_XLA1REPR )
186 : {
187 2 : ::formula::FormulaGrammar::AddressConvention eConv = aNameStr == SC_UNONAME_XLA1REPR ?
188 2 : ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
189 :
190 : // parse the file format string
191 2 : OUString sRepresentation;
192 2 : if (aValue >>= sRepresentation)
193 : {
194 2 : OUString aUIString(sRepresentation);
195 :
196 : // cell or range: strip a single "." at the start
197 2 : if ( aUIString[0]== (sal_Unicode) '.' )
198 0 : aUIString = aUIString.copy( 1 );
199 :
200 2 : if ( bIsRange )
201 : {
202 : // range: also strip a "." after the last colon
203 2 : sal_Int32 nColon = OUString(aUIString).lastIndexOf( (sal_Unicode) ':' );
204 4 : if ( nColon >= 0 && nColon < aUIString.getLength() - 1 &&
205 2 : aUIString[nColon+1] == '.' )
206 0 : aUIString = aUIString.replaceAt( nColon+1, 1, "" );
207 : }
208 :
209 : // parse the rest like a UI string
210 2 : bSuccess = ParseUIString( aUIString, eConv );
211 2 : }
212 : }
213 : else
214 0 : throw beans::UnknownPropertyException();
215 :
216 4 : if ( !bSuccess )
217 0 : throw lang::IllegalArgumentException();
218 4 : }
219 :
220 3 : uno::Any SAL_CALL ScAddressConversionObj::getPropertyValue( const OUString& aPropertyName )
221 : throw(beans::UnknownPropertyException, lang::WrappedTargetException,
222 : uno::RuntimeException, std::exception)
223 : {
224 3 : if ( !pDocShell )
225 0 : throw uno::RuntimeException();
226 :
227 3 : ScDocument& rDoc = pDocShell->GetDocument();
228 3 : uno::Any aRet;
229 :
230 6 : OUString aNameStr(aPropertyName);
231 3 : if ( aNameStr == SC_UNONAME_ADDRESS )
232 : {
233 2 : if ( bIsRange )
234 : {
235 2 : table::CellRangeAddress aRangeAddress;
236 2 : ScUnoConversion::FillApiRange( aRangeAddress, aRange );
237 2 : 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 1 : else if ( aNameStr == SC_UNONAME_REFSHEET )
247 : {
248 0 : aRet <<= nRefSheet;
249 : }
250 1 : else if ( aNameStr == 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 1 : else if ( aNameStr == SC_UNONAME_PERSREPR || aNameStr == SC_UNONAME_XLA1REPR )
264 : {
265 1 : ::formula::FormulaGrammar::AddressConvention eConv = aNameStr == SC_UNONAME_XLA1REPR ?
266 1 : ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
267 :
268 : // generate file format string - always include sheet
269 1 : OUString aFormatStr(aRange.aStart.Format(SCA_VALID | SCA_TAB_3D, &rDoc, eConv));
270 1 : if ( bIsRange )
271 : {
272 : // manually concatenate range so both parts always have the sheet name
273 1 : aFormatStr += ":";
274 1 : sal_uInt16 nFlags = SCA_VALID;
275 1 : if( eConv != ::formula::FormulaGrammar::CONV_XL_A1 )
276 1 : nFlags |= SCA_TAB_3D;
277 1 : OUString aSecond(aRange.aEnd.Format(nFlags, &rDoc, eConv));
278 1 : aFormatStr += aSecond ;
279 : }
280 1 : aRet <<= OUString( aFormatStr );
281 : }
282 : else
283 0 : throw beans::UnknownPropertyException();
284 :
285 6 : 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 156 : }
312 :
313 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|