update dev300-m58
[ooovba.git] / sc / source / ui / vba / vbaname.cxx
blobeb01e88f074acd3b5b52ea6056fa4493c5d64626
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbaname.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <vbahelper/helperdecl.hxx>
32 #include <com/sun/star/table/XCellRange.hpp>
33 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
34 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
36 #include "vbaname.hxx"
37 #include "vbarange.hxx"
38 #include "vbaglobals.hxx"
39 #include <vector>
40 #include <rangenam.hxx>
41 #include <vcl/msgbox.hxx>
42 #include "tabvwsh.hxx"
43 #include "viewdata.hxx"
45 using namespace ::ooo::vba;
46 using namespace ::com::sun::star;
48 ScVbaName::ScVbaName(const css::uno::Reference< ov::XHelperInterface >& xParent,
49 const css::uno::Reference< css::uno::XComponentContext >& xContext,
50 const css::uno::Reference< css::sheet::XNamedRange >& xName,
51 const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
52 const css::uno::Reference< css::frame::XModel >& xModel ):
53 NameImpl_BASE( xParent , xContext ),
54 mxModel( xModel ),
55 mxNamedRange( xName ),
56 mxNames( xNames )
60 ScVbaName::~ScVbaName()
64 css::uno::Reference< ov::excel::XWorksheet >
65 ScVbaName::getWorkSheet() throw (css::uno::RuntimeException)
67 uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
68 return xApplication->getActiveSheet();
71 ::rtl::OUString
72 ScVbaName::getName() throw (css::uno::RuntimeException)
74 String sName;
75 sName += UniString( getWorkSheet()->getName());
76 sName += String::CreateFromAscii("!");
77 sName += UniString ( mxNamedRange->getName() );
78 return ::rtl::OUString( sName );
81 void
82 ScVbaName::setName( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
84 mxNamedRange->setName( rName );
87 ::rtl::OUString
88 ScVbaName::getNameLocal() throw (css::uno::RuntimeException)
90 return getName();
93 void
94 ScVbaName::setNameLocal( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
96 setName( rName );
99 sal_Bool
100 ScVbaName::getVisible() throw (css::uno::RuntimeException)
102 return true;
105 void
106 ScVbaName::setVisible( sal_Bool /*bVisible*/ ) throw (css::uno::RuntimeException)
110 ::rtl::OUString
111 ScVbaName::getValue() throw (css::uno::RuntimeException)
113 ::rtl::OUString sValue = mxNamedRange->getContent();
114 ::rtl::OUString sSheetName = getWorkSheet()->getName();
115 ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( ";" );
116 ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( "," );
117 ::rtl::OUString sResult;
118 sal_Int32 nFrom = 0;
119 sal_Int32 nTo = 0;
120 nTo = sValue.indexOf( sSegmentation, nFrom );
121 while ( nTo != -1 )
123 ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
124 if ( sTmpValue.toChar() == '$' )
126 ::rtl::OUString sTmp = sTmpValue.copy( 1 );
127 sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
128 sResult += sTmp;
129 sResult += sNewSegmentation;
131 nFrom = nTo + 1;
132 nTo = sValue.indexOf( sSegmentation, nFrom );
134 ::rtl::OUString sTmpValue = sValue.copy( nFrom );
135 if ( sTmpValue.toChar() == '$' )
137 ::rtl::OUString sTmp = sTmpValue.copy(1);
138 sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
139 sResult += sTmp;
141 if (sResult.indexOf('=') != 0)
143 sResult = ::rtl::OUString::createFromAscii("=") + sResult;
145 return sResult;
148 void
149 ScVbaName::setValue( const ::rtl::OUString & rValue ) throw (css::uno::RuntimeException)
151 ::rtl::OUString sSheetName = getWorkSheet()->getName();
152 ::rtl::OUString sValue = rValue;
153 ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( "," );
154 ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( ";" );
155 ::rtl::OUString sResult;
156 sal_Int32 nFrom = 0;
157 sal_Int32 nTo = 0;
158 if (sValue.indexOf('=') == 0)
160 ::rtl::OUString sTmp = sValue.copy(1);
161 sValue = sTmp;
163 nTo = sValue.indexOf( sSegmentation, nFrom );
164 while ( nTo != -1 )
166 ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
167 sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
168 if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
170 sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
172 sTmpValue += sNewSegmentation;
173 sResult += sTmpValue;
174 nFrom = nTo + 1;
175 nTo = sValue.indexOf( sSegmentation, nFrom );
177 ::rtl::OUString sTmpValue = sValue.copy( nFrom );
178 sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
179 if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
181 sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
183 sResult += sTmpValue;
184 mxNamedRange->setContent(sResult);
187 ::rtl::OUString
188 ScVbaName::getRefersTo() throw (css::uno::RuntimeException)
190 return getValue();
193 void
194 ScVbaName::setRefersTo( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
196 setValue( rRefersTo );
199 ::rtl::OUString
200 ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException)
202 return getRefersTo();
205 void
206 ScVbaName::setRefersToLocal( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
208 setRefersTo( rRefersTo );
211 ::rtl::OUString
212 ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException)
214 return getRefersTo();
217 void
218 ScVbaName::setRefersToR1C1( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
220 setRefersTo( rRefersTo );
223 ::rtl::OUString
224 ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException)
226 return getRefersTo();
229 void
230 ScVbaName::setRefersToR1C1Local( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
232 setRefersTo( rRefersTo );
235 css::uno::Reference< ov::excel::XRange >
236 ScVbaName::getRefersToRange() throw (css::uno::RuntimeException)
238 uno::Reference< ov::excel::XRange > xRange = ScVbaRange::getRangeObjectForName( mxContext, mxNamedRange->getName(), excel::getDocShell( mxModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
239 return xRange;
242 void
243 ScVbaName::setRefersToRange( const css::uno::Reference< ov::excel::XRange > /*rRange*/ ) throw (css::uno::RuntimeException)
247 void
248 ScVbaName::Delete() throw (css::uno::RuntimeException)
250 mxNames->removeByName( mxNamedRange->getName() );
253 rtl::OUString&
254 ScVbaName::getServiceImplName()
256 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaName") );
257 return sImplName;
260 uno::Sequence< rtl::OUString >
261 ScVbaName::getServiceNames()
263 static uno::Sequence< rtl::OUString > aServiceNames;
264 if ( aServiceNames.getLength() == 0 )
266 aServiceNames.realloc( 1 );
267 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Name" ) );
269 return aServiceNames;