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