xref: /trunk/main/sc/source/ui/vba/vbaname.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include <vbahelper/helperdecl.hxx>
28 
29 #include <com/sun/star/table/XCellRange.hpp>
30 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
31 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
32 
33 #include "vbaname.hxx"
34 #include "vbarange.hxx"
35 #include "vbaglobals.hxx"
36 #include <vector>
37 #include <rangenam.hxx>
38 #include <vcl/msgbox.hxx>
39 #include "tabvwsh.hxx"
40 #include "viewdata.hxx"
41 
42 using namespace ::ooo::vba;
43 using namespace ::com::sun::star;
44 
45 ScVbaName::ScVbaName(const css::uno::Reference< ov::XHelperInterface >& xParent,
46             const css::uno::Reference< css::uno::XComponentContext >& xContext,
47             const css::uno::Reference< css::sheet::XNamedRange >& xName,
48             const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
49             const css::uno::Reference< css::frame::XModel >& xModel ):
50             NameImpl_BASE(  xParent , xContext ),
51             mxModel( xModel ),
52             mxNamedRange( xName ),
53             mxNames( xNames )
54 {
55 }
56 
57 ScVbaName::~ScVbaName()
58 {
59 }
60 
61 css::uno::Reference< ov::excel::XWorksheet >
62 ScVbaName::getWorkSheet() throw (css::uno::RuntimeException)
63 {
64     uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
65     return xApplication->getActiveSheet();
66 }
67 
68 ::rtl::OUString
69 ScVbaName::getName() throw (css::uno::RuntimeException)
70 {
71     String sName;
72     sName += UniString( getWorkSheet()->getName());
73     sName += String::CreateFromAscii("!");
74     sName += UniString ( mxNamedRange->getName() );
75     return ::rtl::OUString( sName );
76 }
77 
78 void
79 ScVbaName::setName( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
80 {
81     mxNamedRange->setName( rName );
82 }
83 
84 ::rtl::OUString
85 ScVbaName::getNameLocal() throw (css::uno::RuntimeException)
86 {
87     return getName();
88 }
89 
90 void
91 ScVbaName::setNameLocal( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
92 {
93     setName( rName );
94 }
95 
96 sal_Bool
97 ScVbaName::getVisible() throw (css::uno::RuntimeException)
98 {
99     return true;
100 }
101 
102 void
103 ScVbaName::setVisible( sal_Bool /*bVisible*/ ) throw (css::uno::RuntimeException)
104 {
105 }
106 
107 ::rtl::OUString
108 ScVbaName::getValue() throw (css::uno::RuntimeException)
109 {
110     ::rtl::OUString sValue = mxNamedRange->getContent();
111     ::rtl::OUString sSheetName = getWorkSheet()->getName();
112     ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( ";" );
113     ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( "," );
114     ::rtl::OUString sResult;
115     sal_Int32 nFrom = 0;
116     sal_Int32 nTo = 0;
117     nTo = sValue.indexOf( sSegmentation, nFrom );
118     while ( nTo != -1 )
119     {
120         ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
121         if ( sTmpValue.toChar() == '$' )
122         {
123             ::rtl::OUString sTmp = sTmpValue.copy( 1 );
124             sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
125             sResult += sTmp;
126             sResult += sNewSegmentation;
127         }
128         nFrom = nTo + 1;
129         nTo = sValue.indexOf( sSegmentation, nFrom );
130     }
131     ::rtl::OUString sTmpValue = sValue.copy( nFrom );
132     if ( sTmpValue.toChar() == '$' )
133     {
134         ::rtl::OUString sTmp = sTmpValue.copy(1);
135         sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
136         sResult += sTmp;
137     }
138     if (sResult.indexOf('=') != 0)
139     {
140         sResult = ::rtl::OUString::createFromAscii("=") + sResult;
141     }
142     return sResult;
143 }
144 
145 void
146 ScVbaName::setValue( const ::rtl::OUString & rValue ) throw (css::uno::RuntimeException)
147 {
148     ::rtl::OUString sSheetName = getWorkSheet()->getName();
149     ::rtl::OUString sValue = rValue;
150     ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( "," );
151     ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( ";" );
152     ::rtl::OUString sResult;
153     sal_Int32 nFrom = 0;
154     sal_Int32 nTo = 0;
155     if (sValue.indexOf('=') == 0)
156     {
157         ::rtl::OUString sTmp = sValue.copy(1);
158         sValue = sTmp;
159     }
160     nTo = sValue.indexOf( sSegmentation, nFrom );
161     while ( nTo != -1 )
162     {
163         ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
164         sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
165         if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
166         {
167             sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
168         }
169         sTmpValue += sNewSegmentation;
170         sResult += sTmpValue;
171         nFrom = nTo + 1;
172         nTo = sValue.indexOf( sSegmentation, nFrom );
173     }
174     ::rtl::OUString sTmpValue = sValue.copy( nFrom );
175     sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
176     if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
177     {
178         sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
179     }
180     sResult += sTmpValue;
181     mxNamedRange->setContent(sResult);
182 }
183 
184 ::rtl::OUString
185 ScVbaName::getRefersTo() throw (css::uno::RuntimeException)
186 {
187     return getValue();
188 }
189 
190 void
191 ScVbaName::setRefersTo( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
192 {
193     setValue( rRefersTo );
194 }
195 
196 ::rtl::OUString
197 ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException)
198 {
199     return getRefersTo();
200 }
201 
202 void
203 ScVbaName::setRefersToLocal( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
204 {
205     setRefersTo( rRefersTo );
206 }
207 
208 ::rtl::OUString
209 ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException)
210 {
211     return getRefersTo();
212 }
213 
214 void
215 ScVbaName::setRefersToR1C1( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
216 {
217     setRefersTo( rRefersTo );
218 }
219 
220 ::rtl::OUString
221 ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException)
222 {
223     return getRefersTo();
224 }
225 
226 void
227 ScVbaName::setRefersToR1C1Local( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
228 {
229     setRefersTo( rRefersTo );
230 }
231 
232 css::uno::Reference< ov::excel::XRange >
233 ScVbaName::getRefersToRange() throw (css::uno::RuntimeException)
234 {
235     uno::Reference< ov::excel::XRange > xRange = ScVbaRange::getRangeObjectForName(
236         mxContext, mxNamedRange->getName(), excel::getDocShell( mxModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
237     return xRange;
238 }
239 
240 void
241 ScVbaName::setRefersToRange( const css::uno::Reference< ov::excel::XRange > /*rRange*/ ) throw (css::uno::RuntimeException)
242 {
243 }
244 
245 void
246 ScVbaName::Delete() throw (css::uno::RuntimeException)
247 {
248     mxNames->removeByName( mxNamedRange->getName() );
249 }
250 
251 rtl::OUString&
252 ScVbaName::getServiceImplName()
253 {
254     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaName") );
255     return sImplName;
256 }
257 
258 uno::Sequence< rtl::OUString >
259 ScVbaName::getServiceNames()
260 {
261     static uno::Sequence< rtl::OUString > aServiceNames;
262     if ( aServiceNames.getLength() == 0 )
263     {
264         aServiceNames.realloc( 1 );
265         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Name" ) );
266     }
267     return aServiceNames;
268 }
269 
270