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