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 : #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
24 :
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 "nameuno.hxx"
34 : #include "compiler.hxx"
35 : #include "tokenarray.hxx"
36 :
37 : #include <boost/scoped_ptr.hpp>
38 :
39 : using namespace ::ooo::vba;
40 : using namespace ::com::sun::star;
41 :
42 0 : ScVbaName::ScVbaName(const css::uno::Reference< ov::XHelperInterface >& xParent,
43 : const css::uno::Reference< css::uno::XComponentContext >& xContext,
44 : const css::uno::Reference< css::sheet::XNamedRange >& xName,
45 : const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
46 : const css::uno::Reference< css::frame::XModel >& xModel ):
47 : NameImpl_BASE( xParent , xContext ),
48 : mxModel( xModel ),
49 : mxNamedRange( xName ),
50 0 : mxNames( xNames )
51 : {
52 0 : }
53 :
54 0 : ScVbaName::~ScVbaName()
55 : {
56 0 : }
57 :
58 : css::uno::Reference< ov::excel::XWorksheet >
59 0 : ScVbaName::getWorkSheet() throw (css::uno::RuntimeException)
60 : {
61 0 : uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
62 0 : return xApplication->getActiveSheet();
63 : }
64 :
65 : OUString
66 0 : ScVbaName::getName() throw (css::uno::RuntimeException, std::exception)
67 : {
68 0 : return mxNamedRange->getName();
69 : }
70 :
71 : void
72 0 : ScVbaName::setName( const OUString & rName ) throw (css::uno::RuntimeException, std::exception)
73 : {
74 0 : mxNamedRange->setName( rName );
75 0 : }
76 :
77 : OUString
78 0 : ScVbaName::getNameLocal() throw (css::uno::RuntimeException, std::exception)
79 : {
80 0 : return getName();
81 : }
82 :
83 : void
84 0 : ScVbaName::setNameLocal( const OUString & rName ) throw (css::uno::RuntimeException, std::exception)
85 : {
86 0 : setName( rName );
87 0 : }
88 :
89 : sal_Bool
90 0 : ScVbaName::getVisible() throw (css::uno::RuntimeException, std::exception)
91 : {
92 0 : return true;
93 : }
94 :
95 : void
96 0 : ScVbaName::setVisible( sal_Bool /*bVisible*/ ) throw (css::uno::RuntimeException, std::exception)
97 : {
98 0 : }
99 :
100 0 : OUString ScVbaName::getContent( const formula::FormulaGrammar::Grammar eGrammar, bool bPrependEquals )
101 : {
102 0 : ScNamedRangeObj* pNamedRange = dynamic_cast< ScNamedRangeObj* >( mxNamedRange.get() );
103 0 : OUString aContent;
104 0 : if ( pNamedRange )
105 : {
106 0 : ScRangeData* pData = pNamedRange->GetRangeData_Impl();
107 0 : if (pData)
108 0 : pData->GetSymbol( aContent, eGrammar );
109 : }
110 0 : if ( bPrependEquals )
111 : {
112 0 : if (aContent.indexOf('=') != 0)
113 0 : aContent = "=" + aContent;
114 : }
115 0 : return aContent;
116 : }
117 :
118 0 : void ScVbaName::setContent( const OUString& rContent, const formula::FormulaGrammar::Grammar eGrammar, bool bRemoveEquals )
119 : {
120 0 : OUString sContent( rContent );
121 0 : if ( bRemoveEquals )
122 : {
123 0 : if (sContent.startsWith("="))
124 0 : sContent = sContent.copy(1);
125 : }
126 0 : ScNamedRangeObj* pNamedRange = dynamic_cast< ScNamedRangeObj* >( mxNamedRange.get() );
127 :
128 : // We should be able to do the below by just setting calling SetCode on pNamedRange
129 : // right?
130 0 : if ( pNamedRange && pNamedRange->pDocShell )
131 : {
132 :
133 0 : ScDocument* pDoc = pNamedRange->pDocShell->GetDocument();
134 0 : ScRangeData* pOldData = pNamedRange->GetRangeData_Impl();
135 0 : if (pOldData)
136 : {
137 : // Shorter way of doing this ?
138 0 : ScCompiler aComp( pDoc, pOldData->GetPos() );
139 0 : aComp.SetGrammar( eGrammar );
140 0 : boost::scoped_ptr<ScTokenArray> pArray(aComp.CompileString(sContent));
141 0 : pOldData->SetCode(*pArray);
142 : }
143 0 : }
144 0 : }
145 :
146 : OUString
147 0 : ScVbaName::getValue() throw (css::uno::RuntimeException, std::exception)
148 : {
149 0 : rtl::OUString sResult = getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_A1, true );
150 :
151 0 : return sResult;
152 : }
153 :
154 : void
155 0 : ScVbaName::setValue( const OUString & rValue ) throw (css::uno::RuntimeException, std::exception)
156 : {
157 0 : setContent( rValue, formula::FormulaGrammar::GRAM_NATIVE_XL_A1, true );
158 0 : }
159 :
160 : OUString
161 0 : ScVbaName::getRefersTo() throw (css::uno::RuntimeException, std::exception)
162 : {
163 0 : return getValue();
164 : }
165 :
166 : void
167 0 : ScVbaName::setRefersTo( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
168 : {
169 0 : setValue( rRefersTo );
170 0 : }
171 :
172 : OUString
173 0 : ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException, std::exception)
174 : {
175 0 : return getRefersTo();
176 : }
177 :
178 : void
179 0 : ScVbaName::setRefersToLocal( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
180 : {
181 0 : setRefersTo( rRefersTo );
182 0 : }
183 :
184 : OUString
185 0 : ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException, std::exception)
186 : {
187 0 : rtl::OUString sResult = getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1, true );
188 0 : return sResult;
189 : }
190 :
191 : void
192 0 : ScVbaName::setRefersToR1C1( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
193 : {
194 0 : setContent( rRefersTo, formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1, true );
195 0 : }
196 :
197 : OUString
198 0 : ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException, std::exception)
199 : {
200 0 : return getRefersToR1C1();
201 : }
202 :
203 : void
204 0 : ScVbaName::setRefersToR1C1Local( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
205 : {
206 0 : setRefersTo( rRefersTo );
207 0 : }
208 :
209 : css::uno::Reference< ov::excel::XRange >
210 0 : ScVbaName::getRefersToRange() throw (css::uno::RuntimeException, std::exception)
211 : {
212 : uno::Reference< ov::excel::XRange > xRange = ScVbaRange::getRangeObjectForName(
213 0 : mxContext, mxNamedRange->getName(), excel::getDocShell( mxModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
214 0 : return xRange;
215 : }
216 :
217 : void
218 0 : ScVbaName::setRefersToRange( const css::uno::Reference< ov::excel::XRange > /*rRange*/ ) throw (css::uno::RuntimeException)
219 : {
220 0 : }
221 :
222 : void
223 0 : ScVbaName::Delete() throw (css::uno::RuntimeException, std::exception)
224 : {
225 0 : mxNames->removeByName( mxNamedRange->getName() );
226 0 : }
227 :
228 : OUString
229 0 : ScVbaName::getServiceImplName()
230 : {
231 0 : return OUString( "ScVbaName" );
232 : }
233 :
234 : uno::Sequence< OUString >
235 0 : ScVbaName::getServiceNames()
236 : {
237 0 : static uno::Sequence< OUString > aServiceNames;
238 0 : if ( aServiceNames.getLength() == 0 )
239 : {
240 0 : aServiceNames.realloc( 1 );
241 0 : aServiceNames[ 0 ] = "ooo.vba.excel.Name";
242 : }
243 0 : return aServiceNames;
244 0 : }
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|