xref: /AOO41X/main/sc/source/ui/unoobj/addruno.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/table/CellAddress.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/table/CellRangeAddress.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <svl/itemprop.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "docsh.hxx"
39*cdf0e10cSrcweir #include "unonames.hxx"
40*cdf0e10cSrcweir #include "unoguard.hxx"
41*cdf0e10cSrcweir #include "miscuno.hxx"
42*cdf0e10cSrcweir #include "convuno.hxx"
43*cdf0e10cSrcweir #include "addruno.hxx"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace com::sun::star;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //------------------------------------------------------------------------
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir ScAddressConversionObj::ScAddressConversionObj(ScDocShell* pDocSh, sal_Bool bForRange) :
50*cdf0e10cSrcweir     pDocShell( pDocSh ),
51*cdf0e10cSrcweir     nRefSheet( 0 ),
52*cdf0e10cSrcweir     bIsRange( bForRange )
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     pDocShell->GetDocument()->AddUnoObject(*this);
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir ScAddressConversionObj::~ScAddressConversionObj()
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir     if (pDocShell)
60*cdf0e10cSrcweir         pDocShell->GetDocument()->RemoveUnoObject(*this);
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir void ScAddressConversionObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir     if ( rHint.ISA( SfxSimpleHint ) &&
66*cdf0e10cSrcweir             ((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         pDocShell = NULL;       // invalid
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir sal_Bool ScAddressConversionObj::ParseUIString( const String& rUIString, ::formula::FormulaGrammar::AddressConvention eConv )
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     if (!pDocShell)
75*cdf0e10cSrcweir         return sal_False;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     ScDocument* pDoc = pDocShell->GetDocument();
78*cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
79*cdf0e10cSrcweir     if ( bIsRange )
80*cdf0e10cSrcweir     {
81*cdf0e10cSrcweir         sal_uInt16 nResult = aRange.ParseAny( rUIString, pDoc, eConv );
82*cdf0e10cSrcweir         if ( nResult & SCA_VALID )
83*cdf0e10cSrcweir         {
84*cdf0e10cSrcweir             if ( ( nResult & SCA_TAB_3D ) == 0 )
85*cdf0e10cSrcweir                 aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
86*cdf0e10cSrcweir             if ( ( nResult & SCA_TAB2_3D ) == 0 )
87*cdf0e10cSrcweir                 aRange.aEnd.SetTab( aRange.aStart.Tab() );
88*cdf0e10cSrcweir             // different sheets are not supported in CellRangeAddress
89*cdf0e10cSrcweir             if ( aRange.aStart.Tab() == aRange.aEnd.Tab() )
90*cdf0e10cSrcweir                 bSuccess = sal_True;
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir     }
93*cdf0e10cSrcweir     else
94*cdf0e10cSrcweir     {
95*cdf0e10cSrcweir         sal_uInt16 nResult = aRange.aStart.Parse( rUIString, pDoc, eConv );
96*cdf0e10cSrcweir         if ( nResult & SCA_VALID )
97*cdf0e10cSrcweir         {
98*cdf0e10cSrcweir             if ( ( nResult & SCA_TAB_3D ) == 0 )
99*cdf0e10cSrcweir                 aRange.aStart.SetTab( static_cast<SCTAB>(nRefSheet) );
100*cdf0e10cSrcweir             bSuccess = sal_True;
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir     return bSuccess;
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir // XPropertySet
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir uno::Reference<beans::XPropertySetInfo> SAL_CALL ScAddressConversionObj::getPropertySetInfo()
109*cdf0e10cSrcweir                                                         throw(uno::RuntimeException)
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir     ScUnoGuard aGuard;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     if ( bIsRange )
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         static SfxItemPropertyMapEntry aPropertyMap[] =
116*cdf0e10cSrcweir         {
117*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_ADDRESS),  0,  &getCppuType((table::CellRangeAddress*)0), 0, 0 },
118*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_PERSREPR), 0,  &getCppuType((rtl::OUString*)0),    0, 0 },
119*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_REFSHEET), 0,  &getCppuType((sal_Int32*)0),        0, 0 },
120*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_UIREPR),   0,  &getCppuType((rtl::OUString*)0),    0, 0 },
121*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_XLA1REPR), 0,  &getCppuType((rtl::OUString*)0),    0, 0 },
122*cdf0e10cSrcweir             {0,0,0,0,0,0}
123*cdf0e10cSrcweir         };
124*cdf0e10cSrcweir         static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
125*cdf0e10cSrcweir         return aRef;
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir     else
128*cdf0e10cSrcweir     {
129*cdf0e10cSrcweir         static SfxItemPropertyMapEntry aPropertyMap[] =
130*cdf0e10cSrcweir         {
131*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_ADDRESS),  0,  &getCppuType((table::CellAddress*)0), 0, 0 },
132*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_PERSREPR), 0,  &getCppuType((rtl::OUString*)0),    0, 0 },
133*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_REFSHEET), 0,  &getCppuType((sal_Int32*)0),        0, 0 },
134*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_UIREPR),   0,  &getCppuType((rtl::OUString*)0),    0, 0 },
135*cdf0e10cSrcweir             {MAP_CHAR_LEN(SC_UNONAME_XLA1REPR), 0,  &getCppuType((rtl::OUString*)0),    0, 0 },
136*cdf0e10cSrcweir             {0,0,0,0,0,0}
137*cdf0e10cSrcweir         };
138*cdf0e10cSrcweir         static uno::Reference<beans::XPropertySetInfo> aRef(new SfxItemPropertySetInfo( aPropertyMap ));
139*cdf0e10cSrcweir         return aRef;
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir void SAL_CALL ScAddressConversionObj::setPropertyValue( const rtl::OUString& aPropertyName, const uno::Any& aValue )
144*cdf0e10cSrcweir                 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
145*cdf0e10cSrcweir                         lang::IllegalArgumentException, lang::WrappedTargetException,
146*cdf0e10cSrcweir                         uno::RuntimeException)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir     if ( !pDocShell )
149*cdf0e10cSrcweir         throw uno::RuntimeException();
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
152*cdf0e10cSrcweir     String aNameStr(aPropertyName);
153*cdf0e10cSrcweir     if ( aNameStr.EqualsAscii( SC_UNONAME_ADDRESS ) )
154*cdf0e10cSrcweir     {
155*cdf0e10cSrcweir         //  read the cell/range address from API struct
156*cdf0e10cSrcweir         if ( bIsRange )
157*cdf0e10cSrcweir         {
158*cdf0e10cSrcweir             table::CellRangeAddress aRangeAddress;
159*cdf0e10cSrcweir             if ( aValue >>= aRangeAddress )
160*cdf0e10cSrcweir             {
161*cdf0e10cSrcweir                 ScUnoConversion::FillScRange( aRange, aRangeAddress );
162*cdf0e10cSrcweir                 bSuccess = sal_True;
163*cdf0e10cSrcweir             }
164*cdf0e10cSrcweir         }
165*cdf0e10cSrcweir         else
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             table::CellAddress aCellAddress;
168*cdf0e10cSrcweir             if ( aValue >>= aCellAddress )
169*cdf0e10cSrcweir             {
170*cdf0e10cSrcweir                 ScUnoConversion::FillScAddress( aRange.aStart, aCellAddress );
171*cdf0e10cSrcweir                 bSuccess = sal_True;
172*cdf0e10cSrcweir             }
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir     else if ( aNameStr.EqualsAscii( SC_UNONAME_REFSHEET ) )
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         //  set the reference sheet
178*cdf0e10cSrcweir         sal_Int32 nIntVal = 0;
179*cdf0e10cSrcweir         if ( aValue >>= nIntVal )
180*cdf0e10cSrcweir         {
181*cdf0e10cSrcweir             nRefSheet = nIntVal;
182*cdf0e10cSrcweir             bSuccess = sal_True;
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir     else if ( aNameStr.EqualsAscii( SC_UNONAME_UIREPR ) )
186*cdf0e10cSrcweir     {
187*cdf0e10cSrcweir         //  parse the UI representation string
188*cdf0e10cSrcweir         rtl::OUString sRepresentation;
189*cdf0e10cSrcweir         if (aValue >>= sRepresentation)
190*cdf0e10cSrcweir         {
191*cdf0e10cSrcweir             String aUIString = sRepresentation;
192*cdf0e10cSrcweir             bSuccess = ParseUIString( aUIString );
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir     else if ( aNameStr.EqualsAscii( SC_UNONAME_PERSREPR ) || aNameStr.EqualsAscii( SC_UNONAME_XLA1REPR ) )
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         ::formula::FormulaGrammar::AddressConvention eConv = aNameStr.EqualsAscii( SC_UNONAME_XLA1REPR ) ?
198*cdf0e10cSrcweir             ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         //  parse the file format string
201*cdf0e10cSrcweir         rtl::OUString sRepresentation;
202*cdf0e10cSrcweir         if (aValue >>= sRepresentation)
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             String aUIString(sRepresentation);
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir             //  cell or range: strip a single "." at the start
207*cdf0e10cSrcweir             if ( aUIString.GetChar(0) == (sal_Unicode) '.' )
208*cdf0e10cSrcweir                 aUIString.Erase( 0, 1 );
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir             if ( bIsRange )
211*cdf0e10cSrcweir             {
212*cdf0e10cSrcweir                 //  range: also strip a "." after the last colon
213*cdf0e10cSrcweir                 sal_Int32 nColon = rtl::OUString(aUIString).lastIndexOf( (sal_Unicode) ':' );
214*cdf0e10cSrcweir                 if ( nColon >= 0 && nColon < aUIString.Len() - 1 &&
215*cdf0e10cSrcweir                      aUIString.GetChar((xub_StrLen)nColon+1) == (sal_Unicode) '.' )
216*cdf0e10cSrcweir                     aUIString.Erase( (xub_StrLen)nColon+1, 1 );
217*cdf0e10cSrcweir             }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir             //  parse the rest like a UI string
220*cdf0e10cSrcweir             bSuccess = ParseUIString( aUIString, eConv );
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir     }
223*cdf0e10cSrcweir     else
224*cdf0e10cSrcweir         throw beans::UnknownPropertyException();
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     if ( !bSuccess )
227*cdf0e10cSrcweir         throw lang::IllegalArgumentException();
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir uno::Any SAL_CALL ScAddressConversionObj::getPropertyValue( const rtl::OUString& aPropertyName )
231*cdf0e10cSrcweir                 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
232*cdf0e10cSrcweir                         uno::RuntimeException)
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir     if ( !pDocShell )
235*cdf0e10cSrcweir         throw uno::RuntimeException();
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     ScDocument* pDoc = pDocShell->GetDocument();
238*cdf0e10cSrcweir     uno::Any aRet;
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     String aNameStr(aPropertyName);
241*cdf0e10cSrcweir     if ( aNameStr.EqualsAscii( SC_UNONAME_ADDRESS ) )
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         if ( bIsRange )
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             table::CellRangeAddress aRangeAddress;
246*cdf0e10cSrcweir             ScUnoConversion::FillApiRange( aRangeAddress, aRange );
247*cdf0e10cSrcweir             aRet <<= aRangeAddress;
248*cdf0e10cSrcweir         }
249*cdf0e10cSrcweir         else
250*cdf0e10cSrcweir         {
251*cdf0e10cSrcweir             table::CellAddress aCellAddress;
252*cdf0e10cSrcweir             ScUnoConversion::FillApiAddress( aCellAddress, aRange.aStart );
253*cdf0e10cSrcweir             aRet <<= aCellAddress;
254*cdf0e10cSrcweir         }
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir     else if ( aNameStr.EqualsAscii( SC_UNONAME_REFSHEET ) )
257*cdf0e10cSrcweir     {
258*cdf0e10cSrcweir         aRet <<= nRefSheet;
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir     else if ( aNameStr.EqualsAscii( SC_UNONAME_UIREPR ) )
261*cdf0e10cSrcweir     {
262*cdf0e10cSrcweir         //  generate UI representation string - include sheet only if different from ref sheet
263*cdf0e10cSrcweir         String aFormatStr;
264*cdf0e10cSrcweir         sal_uInt16 nFlags = SCA_VALID;
265*cdf0e10cSrcweir         if ( aRange.aStart.Tab() != nRefSheet )
266*cdf0e10cSrcweir             nFlags |= SCA_TAB_3D;
267*cdf0e10cSrcweir         if ( bIsRange )
268*cdf0e10cSrcweir             aRange.Format( aFormatStr, nFlags, pDoc );
269*cdf0e10cSrcweir         else
270*cdf0e10cSrcweir             aRange.aStart.Format( aFormatStr, nFlags, pDoc );
271*cdf0e10cSrcweir         aRet <<= rtl::OUString( aFormatStr );
272*cdf0e10cSrcweir     }
273*cdf0e10cSrcweir     else if ( aNameStr.EqualsAscii( SC_UNONAME_PERSREPR ) || aNameStr.EqualsAscii( SC_UNONAME_XLA1REPR ) )
274*cdf0e10cSrcweir     {
275*cdf0e10cSrcweir         ::formula::FormulaGrammar::AddressConvention eConv = aNameStr.EqualsAscii( SC_UNONAME_XLA1REPR ) ?
276*cdf0e10cSrcweir             ::formula::FormulaGrammar::CONV_XL_A1 : ::formula::FormulaGrammar::CONV_OOO;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         //  generate file format string - always include sheet
279*cdf0e10cSrcweir         String aFormatStr;
280*cdf0e10cSrcweir         aRange.aStart.Format( aFormatStr, SCA_VALID | SCA_TAB_3D, pDoc, eConv );
281*cdf0e10cSrcweir         if ( bIsRange )
282*cdf0e10cSrcweir         {
283*cdf0e10cSrcweir             //  manually concatenate range so both parts always have the sheet name
284*cdf0e10cSrcweir             aFormatStr.Append( (sal_Unicode) ':' );
285*cdf0e10cSrcweir             String aSecond;
286*cdf0e10cSrcweir             sal_uInt16 nFlags = SCA_VALID;
287*cdf0e10cSrcweir             if( eConv != ::formula::FormulaGrammar::CONV_XL_A1 )
288*cdf0e10cSrcweir                 nFlags |= SCA_TAB_3D;
289*cdf0e10cSrcweir             aRange.aEnd.Format( aSecond, nFlags, pDoc, eConv );
290*cdf0e10cSrcweir             aFormatStr.Append( aSecond );
291*cdf0e10cSrcweir         }
292*cdf0e10cSrcweir         aRet <<= rtl::OUString( aFormatStr );
293*cdf0e10cSrcweir     }
294*cdf0e10cSrcweir     else
295*cdf0e10cSrcweir         throw beans::UnknownPropertyException();
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir     return aRet;
298*cdf0e10cSrcweir }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir SC_IMPL_DUMMY_PROPERTY_LISTENER( ScAddressConversionObj )
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir // lang::XServiceInfo
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir rtl::OUString SAL_CALL ScAddressConversionObj::getImplementationName() throw(uno::RuntimeException)
305*cdf0e10cSrcweir {
306*cdf0e10cSrcweir     return rtl::OUString::createFromAscii( "ScAddressConversionObj" );
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir sal_Bool SAL_CALL ScAddressConversionObj::supportsService( const rtl::OUString& rServiceName )
310*cdf0e10cSrcweir                                                     throw(uno::RuntimeException)
311*cdf0e10cSrcweir {
312*cdf0e10cSrcweir     String aServiceStr( rServiceName );
313*cdf0e10cSrcweir     return aServiceStr.EqualsAscii( bIsRange ? SC_SERVICENAME_RANGEADDRESS
314*cdf0e10cSrcweir                                              : SC_SERVICENAME_CELLADDRESS );
315*cdf0e10cSrcweir }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir uno::Sequence<rtl::OUString> SAL_CALL ScAddressConversionObj::getSupportedServiceNames()
318*cdf0e10cSrcweir                                                     throw(uno::RuntimeException)
319*cdf0e10cSrcweir {
320*cdf0e10cSrcweir     uno::Sequence<rtl::OUString> aRet(1);
321*cdf0e10cSrcweir     rtl::OUString* pArray = aRet.getArray();
322*cdf0e10cSrcweir     pArray[0] = rtl::OUString::createFromAscii( bIsRange ? SC_SERVICENAME_RANGEADDRESS
323*cdf0e10cSrcweir                                                          : SC_SERVICENAME_CELLADDRESS );
324*cdf0e10cSrcweir     return aRet;
325*cdf0e10cSrcweir }
326*cdf0e10cSrcweir 
327