xref: /AOO41X/main/sc/source/ui/vba/vbanames.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 #include <vbahelper/helperdecl.hxx>
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <com/sun/star/table/XCellRange.hpp>
30*cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "vbanames.hxx"
33*cdf0e10cSrcweir #include "vbaname.hxx"
34*cdf0e10cSrcweir #include "vbarange.hxx"
35*cdf0e10cSrcweir #include "vbaglobals.hxx"
36*cdf0e10cSrcweir #include <vector>
37*cdf0e10cSrcweir #include <rangenam.hxx>
38*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
39*cdf0e10cSrcweir #include "tabvwsh.hxx"
40*cdf0e10cSrcweir #include "viewdata.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace ::ooo::vba;
43*cdf0e10cSrcweir using namespace ::com::sun::star;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir class NamesEnumeration : public EnumerationHelperImpl
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 	uno::Reference< frame::XModel > m_xModel;
48*cdf0e10cSrcweir 	uno::WeakReference< XHelperInterface > m_xParent;
49*cdf0e10cSrcweir 	uno::Reference< sheet::XNamedRanges > m_xNames;
50*cdf0e10cSrcweir public:
51*cdf0e10cSrcweir     NamesEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration,  const uno::Reference< frame::XModel >& xModel , const uno::Reference< sheet::XNamedRanges >& xNames ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel( xModel ), m_xParent( xParent ), m_xNames( xNames ) {}
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
54*cdf0e10cSrcweir 	{
55*cdf0e10cSrcweir 		uno::Reference< sheet::XNamedRange > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
56*cdf0e10cSrcweir 		return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( m_xParent, m_xContext, xNamed ,m_xNames , m_xModel ) ) );
57*cdf0e10cSrcweir 	}
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir };
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir ScVbaNames::ScVbaNames(const css::uno::Reference< ov::XHelperInterface >& xParent,
63*cdf0e10cSrcweir 			const css::uno::Reference< css::uno::XComponentContext >& xContext,
64*cdf0e10cSrcweir 			const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
65*cdf0e10cSrcweir 			const css::uno::Reference< css::frame::XModel >& xModel ):
66*cdf0e10cSrcweir 			ScVbaNames_BASE(  xParent , xContext , uno::Reference< container::XIndexAccess >( xNames, uno::UNO_QUERY ) ),
67*cdf0e10cSrcweir 			mxModel( xModel ),
68*cdf0e10cSrcweir 			mxNames( xNames )
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir     m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir ScVbaNames::~ScVbaNames()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir ScDocument *
78*cdf0e10cSrcweir ScVbaNames::getScDocument()
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_QUERY_THROW );
81*cdf0e10cSrcweir 	ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
82*cdf0e10cSrcweir 	if ( !pTabViewShell )
83*cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewShell available"), uno::Reference< uno::XInterface >() );
84*cdf0e10cSrcweir 	ScViewData* pViewData = pTabViewShell->GetViewData();
85*cdf0e10cSrcweir 	if ( !pViewData )
86*cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewData available"), uno::Reference< uno::XInterface >() );
87*cdf0e10cSrcweir 	return pViewData->GetDocument();
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir css::uno::Any
91*cdf0e10cSrcweir ScVbaNames::Add( const css::uno::Any& Name ,
92*cdf0e10cSrcweir                                         const css::uno::Any& RefersTo,
93*cdf0e10cSrcweir                                         const css::uno::Any& /*Visible*/,
94*cdf0e10cSrcweir                                         const css::uno::Any& /*MacroType*/,
95*cdf0e10cSrcweir                                         const css::uno::Any& /*ShoutcutKey*/,
96*cdf0e10cSrcweir                                         const css::uno::Any& /*Category*/,
97*cdf0e10cSrcweir                                         const css::uno::Any& NameLocal,
98*cdf0e10cSrcweir                                         const css::uno::Any& /*RefersToLocal*/,
99*cdf0e10cSrcweir                                         const css::uno::Any& /*CategoryLocal*/,
100*cdf0e10cSrcweir                                         const css::uno::Any& RefersToR1C1,
101*cdf0e10cSrcweir                                         const css::uno::Any& RefersToR1C1Local ) throw (css::uno::RuntimeException)
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir 	rtl::OUString sName;
104*cdf0e10cSrcweir 	uno::Reference< excel::XRange > xRange;
105*cdf0e10cSrcweir 	if ( Name.hasValue() )
106*cdf0e10cSrcweir 		Name >>= sName;
107*cdf0e10cSrcweir 	else if ( NameLocal.hasValue() )
108*cdf0e10cSrcweir 		NameLocal >>= sName;
109*cdf0e10cSrcweir 	if ( sName.getLength() != 0 )
110*cdf0e10cSrcweir 	{
111*cdf0e10cSrcweir 		if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
112*cdf0e10cSrcweir 		{
113*cdf0e10cSrcweir 			::rtl::OUString sResult ;
114*cdf0e10cSrcweir 			sal_Int32 nToken = 0;
115*cdf0e10cSrcweir 			sal_Int32 nIndex = 0;
116*cdf0e10cSrcweir 			sResult = sName.getToken( nToken , '!' , nIndex );
117*cdf0e10cSrcweir 			if ( -1 == nIndex )
118*cdf0e10cSrcweir 				sResult = sName;
119*cdf0e10cSrcweir 			else
120*cdf0e10cSrcweir 				sResult = sName.copy( nIndex );
121*cdf0e10cSrcweir 			sName = sResult ;
122*cdf0e10cSrcweir 			if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
123*cdf0e10cSrcweir 				throw uno::RuntimeException( rtl::OUString::createFromAscii("This Name is a valid ."), uno::Reference< uno::XInterface >() );
124*cdf0e10cSrcweir 		}
125*cdf0e10cSrcweir 	}
126*cdf0e10cSrcweir 	if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		if ( RefersTo.hasValue() )
129*cdf0e10cSrcweir 			RefersTo >>= xRange;
130*cdf0e10cSrcweir 		if ( RefersToR1C1.hasValue() )
131*cdf0e10cSrcweir 			RefersToR1C1 >>= xRange;
132*cdf0e10cSrcweir 		if ( RefersToR1C1Local.hasValue() )
133*cdf0e10cSrcweir 			RefersToR1C1Local >>= xRange;
134*cdf0e10cSrcweir 	}
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 	if ( xRange.is() )
137*cdf0e10cSrcweir 	{
138*cdf0e10cSrcweir 		ScVbaRange* pRange = dynamic_cast< ScVbaRange* >( xRange.get() );
139*cdf0e10cSrcweir 		uno::Reference< table::XCellRange > thisRange ;
140*cdf0e10cSrcweir 		uno::Any xAny = pRange->getCellRange() ;
141*cdf0e10cSrcweir 		if ( xAny.hasValue() )
142*cdf0e10cSrcweir 			xAny >>= thisRange;
143*cdf0e10cSrcweir 		uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( thisRange, ::uno::UNO_QUERY_THROW);
144*cdf0e10cSrcweir 		table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
145*cdf0e10cSrcweir 		ScAddress aPos( static_cast< SCCOL >( aAddr.StartColumn ) , static_cast< SCROW >( aAddr.StartRow ) , static_cast< SCTAB >(aAddr.Sheet ) );
146*cdf0e10cSrcweir 		uno::Any xAny2 ;
147*cdf0e10cSrcweir 		String sRangeAdd = xRange->Address( xAny2, xAny2 , xAny2 , xAny2, xAny2 );
148*cdf0e10cSrcweir 		String sTmp;
149*cdf0e10cSrcweir 		sTmp += String::CreateFromAscii("$");
150*cdf0e10cSrcweir 		sTmp += UniString(xRange->getWorksheet()->getName());
151*cdf0e10cSrcweir 		sTmp += String::CreateFromAscii(".");
152*cdf0e10cSrcweir 		sTmp += sRangeAdd;
153*cdf0e10cSrcweir 		if ( mxNames.is() )
154*cdf0e10cSrcweir 		{
155*cdf0e10cSrcweir 			RangeType nType = RT_NAME;
156*cdf0e10cSrcweir 			table::CellAddress aCellAddr( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
157*cdf0e10cSrcweir 			if ( mxNames->hasByName( sName ) )
158*cdf0e10cSrcweir 				mxNames->removeByName(sName);
159*cdf0e10cSrcweir 			mxNames->addNewByName( sName , rtl::OUString(sTmp) , aCellAddr , (sal_Int32)nType);
160*cdf0e10cSrcweir 			uno::Reference< sheet::XNamedRange > xName( mxNames->getByName( sName ), uno::UNO_QUERY_THROW );
161*cdf0e10cSrcweir             return uno::Any( uno::Reference< excel::XName >( new ScVbaName( getParent(), mxContext, xName, mxNames, mxModel ) ) );
162*cdf0e10cSrcweir 		}
163*cdf0e10cSrcweir 	}
164*cdf0e10cSrcweir 	return css::uno::Any();
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir // XEnumerationAccess
168*cdf0e10cSrcweir css::uno::Type
169*cdf0e10cSrcweir ScVbaNames::getElementType() throw( css::uno::RuntimeException )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir 	return ov::excel::XName::static_type(0);
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir uno::Reference< container::XEnumeration >
175*cdf0e10cSrcweir ScVbaNames::createEnumeration() throw (uno::RuntimeException)
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir 	uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
178*cdf0e10cSrcweir 	return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir uno::Any
182*cdf0e10cSrcweir ScVbaNames::createCollectionObject( const uno::Any& aSource )
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
185*cdf0e10cSrcweir 	return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( getParent(), mxContext, xName, mxNames , mxModel ) ) );
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir rtl::OUString&
189*cdf0e10cSrcweir ScVbaNames::getServiceImplName()
190*cdf0e10cSrcweir {
191*cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaNames") );
192*cdf0e10cSrcweir 	return sImplName;
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir css::uno::Sequence<rtl::OUString>
196*cdf0e10cSrcweir ScVbaNames::getServiceNames()
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
199*cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
200*cdf0e10cSrcweir 	{
201*cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
202*cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.NamedRanges" ) );
203*cdf0e10cSrcweir 	}
204*cdf0e10cSrcweir 	return aServiceNames;
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 
208