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