GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / sc / source / ui / vba / vbanames.cxx
blobc3001c4b778983f3cc546c486c3fc40b45ae080b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <vbahelper/helperdecl.hxx>
21 #include <com/sun/star/table/XCellRange.hpp>
22 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
24 #include "vbanames.hxx"
25 #include "vbaname.hxx"
26 #include "vbarange.hxx"
27 #include "vbaglobals.hxx"
28 #include <vector>
29 #include <rangenam.hxx>
30 #include <vcl/msgbox.hxx>
31 #include "tabvwsh.hxx"
32 #include "viewdata.hxx"
33 #include "compiler.hxx"
34 #include "tokenarray.hxx"
35 #include "cellsuno.hxx"
37 #include <boost/scoped_ptr.hpp>
39 using namespace ::ooo::vba;
40 using namespace ::com::sun::star;
42 class NamesEnumeration : public EnumerationHelperImpl
44 uno::Reference< frame::XModel > m_xModel;
45 uno::WeakReference< XHelperInterface > m_xParent;
46 uno::Reference< sheet::XNamedRanges > m_xNames;
47 public:
48 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 ) {}
50 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
52 uno::Reference< sheet::XNamedRange > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
53 return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( m_xParent, m_xContext, xNamed ,m_xNames , m_xModel ) ) );
59 ScVbaNames::ScVbaNames(const css::uno::Reference< ov::XHelperInterface >& xParent,
60 const css::uno::Reference< css::uno::XComponentContext >& xContext,
61 const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
62 const css::uno::Reference< css::frame::XModel >& xModel ):
63 ScVbaNames_BASE( xParent , xContext , uno::Reference< container::XIndexAccess >( xNames, uno::UNO_QUERY ) ),
64 mxModel( xModel ),
65 mxNames( xNames )
67 m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
70 ScVbaNames::~ScVbaNames()
74 ScDocument *
75 ScVbaNames::getScDocument()
77 uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_QUERY_THROW );
78 ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
79 if ( !pTabViewShell )
80 throw uno::RuntimeException( "No ViewShell available", uno::Reference< uno::XInterface >() );
81 ScViewData* pViewData = pTabViewShell->GetViewData();
82 if ( !pViewData )
83 throw uno::RuntimeException( "No ViewData available", uno::Reference< uno::XInterface >() );
84 return pViewData->GetDocument();
88 css::uno::Any
89 ScVbaNames::Add( const css::uno::Any& Name ,
90 const css::uno::Any& RefersTo,
91 const css::uno::Any& /*Visible*/,
92 const css::uno::Any& /*MacroType*/,
93 const css::uno::Any& /*ShoutcutKey*/,
94 const css::uno::Any& /*Category*/,
95 const css::uno::Any& NameLocal,
96 const css::uno::Any& /*RefersToLocal*/,
97 const css::uno::Any& /*CategoryLocal*/,
98 const css::uno::Any& RefersToR1C1,
99 const css::uno::Any& RefersToR1C1Local ) throw (css::uno::RuntimeException)
101 OUString sName;
102 uno::Reference< excel::XRange > xRange;
103 if ( Name.hasValue() )
104 Name >>= sName;
105 else if ( NameLocal.hasValue() )
106 NameLocal >>= sName;
107 if ( !sName.isEmpty() )
109 if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
111 OUString sResult ;
112 sal_Int32 nToken = 0;
113 sal_Int32 nIndex = 0;
114 sResult = sName.getToken( nToken , '!' , nIndex );
115 if ( -1 == nIndex )
116 sResult = sName;
117 else
118 sResult = sName.copy( nIndex );
119 sName = sResult ;
120 if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
121 throw uno::RuntimeException( "This Name is not valid .",
122 uno::Reference< uno::XInterface >() );
125 uno::Reference< table::XCellRange > xUnoRange;
126 if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
128 OUString sFormula;
130 formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
131 if ( RefersTo.hasValue() )
133 if ( RefersTo.getValueTypeClass() == uno::TypeClass_STRING )
134 RefersTo >>= sFormula;
135 else
136 RefersTo >>= xRange;
138 if ( RefersToR1C1.hasValue() )
140 if ( RefersToR1C1.getValueTypeClass() == uno::TypeClass_STRING )
142 RefersToR1C1 >>= sFormula;
143 eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
145 else
146 RefersToR1C1 >>= xRange;
148 if ( RefersToR1C1Local.hasValue() )
150 if ( RefersToR1C1Local.getValueTypeClass() == uno::TypeClass_STRING )
152 RefersToR1C1Local >>= sFormula;
153 eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
155 else
156 RefersToR1C1Local >>= xRange;
158 if ( !xRange.is() && !sFormula.isEmpty() )
160 ScAddress aBlank;
161 ScCompiler aComp( getScDocument(), aBlank );
162 aComp.SetGrammar( eGram );
163 boost::scoped_ptr<ScTokenArray> pTokens(aComp.CompileString(sFormula));
164 if ( pTokens )
166 ScRange aRange;
167 ScDocShell* pDocSh = excel::getDocShell(getModel());
168 if (pTokens->IsValidReference(aRange, aBlank))
169 xUnoRange = new ScCellRangeObj( pDocSh, aRange );
170 else
172 // assume it's an address try strip the '=' if it's there
173 // and try and create a range ( must be a better way )
174 if ( sFormula.startsWith("=") )
175 sFormula = sFormula.copy(1);
176 ScRangeList aCellRanges;
177 sal_uInt16 nFlags = 0;
179 formula::FormulaGrammar::AddressConvention eConv = ( eGram == formula::FormulaGrammar::GRAM_NATIVE_XL_A1 ) ? formula::FormulaGrammar::CONV_XL_A1 : formula::FormulaGrammar::CONV_XL_R1C1;
180 if ( ScVbaRange::getCellRangesForAddress( nFlags, sFormula, pDocSh, aCellRanges, eConv , ',' ) )
182 if ( aCellRanges.size() == 1 )
183 xUnoRange = new ScCellRangeObj( pDocSh, *aCellRanges.front() );
184 else
186 uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( pDocSh, aCellRanges ) );
187 xRange = new ScVbaRange( mxParent, mxContext, xRanges );
196 if ( xRange.is() || xUnoRange.is() )
198 if ( !xRange.is() )
199 xRange = new ScVbaRange( mxParent, mxContext, xUnoRange );
201 uno::Reference< excel::XRange > xArea( xRange->Areas( uno::makeAny( sal_Int32(1) ) ), uno::UNO_QUERY );
203 uno::Any xAny = xArea->getCellRange() ;
205 uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( xAny, ::uno::UNO_QUERY_THROW);
207 table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
208 ScAddress aPos( static_cast< SCCOL >( aAddr.StartColumn ) , static_cast< SCROW >( aAddr.StartRow ) , static_cast< SCTAB >(aAddr.Sheet ) );
209 uno::Any xAny2 ;
210 if ( mxNames.is() )
212 RangeType nType = RT_NAME;
213 table::CellAddress aCellAddr( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
214 if ( mxNames->hasByName( sName ) )
215 mxNames->removeByName(sName);
216 OUString sTmp = "$";
217 uno::Reference< ov::XCollection > xCol( xRange->Areas( uno::Any() ), uno::UNO_QUERY );
218 for ( sal_Int32 nArea = 1; nArea <= xCol->getCount(); ++nArea )
220 xArea.set( xRange->Areas( uno::makeAny( nArea ) ), uno::UNO_QUERY_THROW );
222 OUString sRangeAdd = xArea->Address( xAny2, xAny2 , xAny2 , xAny2, xAny2 );
223 if ( nArea > 1 )
224 sTmp += ",";
225 sTmp = sTmp + "'" + xRange->getWorksheet()->getName() + "'." + sRangeAdd;
227 mxNames->addNewByName( sName , sTmp , aCellAddr , (sal_Int32)nType);
228 return Item( uno::makeAny( sName ), uno::Any() );
231 return css::uno::Any();
234 // XEnumerationAccess
235 css::uno::Type
236 ScVbaNames::getElementType() throw( css::uno::RuntimeException )
238 return ov::excel::XName::static_type(0);
241 uno::Reference< container::XEnumeration >
242 ScVbaNames::createEnumeration() throw (uno::RuntimeException)
244 uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
245 return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
248 uno::Any
249 ScVbaNames::createCollectionObject( const uno::Any& aSource )
251 uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
252 return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( getParent(), mxContext, xName, mxNames , mxModel ) ) );
255 OUString
256 ScVbaNames::getServiceImplName()
258 return OUString( "ScVbaNames" );
261 css::uno::Sequence<OUString>
262 ScVbaNames::getServiceNames()
264 static uno::Sequence< OUString > aServiceNames;
265 if ( aServiceNames.getLength() == 0 )
267 aServiceNames.realloc( 1 );
268 aServiceNames[ 0 ] = "ooo.vba.excel.NamedRanges";
270 return aServiceNames;
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */