xref: /aoo4110/main/sc/source/ui/vba/vbaglobals.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include <vbahelper/helperdecl.hxx>
24 #include "vbaglobals.hxx"
25 
26 #include <comphelper/unwrapargs.hxx>
27 
28 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <cppuhelper/component_context.hxx>
31 
32 #include "vbaapplication.hxx"
33 #include "vbaworksheet.hxx"
34 #include "vbarange.hxx"
35 #include <cppuhelper/bootstrap.hxx>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::ooo::vba;
39 
40 
41 
42 // =============================================================================
43 // ScVbaGlobals
44 // =============================================================================
45 
46 //ScVbaGlobals::ScVbaGlobals(  css::uno::Reference< css::uno::XComponentContext >const& rxContext,  ) : ScVbaGlobals_BASE( uno::Reference< XHelperInterface >(), rxContext )
47 rtl::OUString sDocCtxName( RTL_CONSTASCII_USTRINGPARAM("ExcelDocumentContext") );
48 
ScVbaGlobals(uno::Sequence<uno::Any> const & aArgs,uno::Reference<uno::XComponentContext> const & rxContext)49 ScVbaGlobals::ScVbaGlobals( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& rxContext ) : ScVbaGlobals_BASE( uno::Reference< XHelperInterface >(), rxContext, sDocCtxName )
50 {
51 	OSL_TRACE("ScVbaGlobals::ScVbaGlobals()");
52 
53         uno::Sequence< beans::PropertyValue > aInitArgs( 2 );
54         aInitArgs[ 0 ].Name = rtl::OUString::createFromAscii("Application");
55         aInitArgs[ 0 ].Value = uno::makeAny( getApplication() );
56         aInitArgs[ 1 ].Name = sDocCtxName;
57         aInitArgs[ 1 ].Value = uno::makeAny( getXSomethingFromArgs< frame::XModel >( aArgs, 0 ) );
58 
59         init( aInitArgs );
60 }
61 
~ScVbaGlobals()62 ScVbaGlobals::~ScVbaGlobals()
63 {
64 	OSL_TRACE("ScVbaGlobals::~ScVbaGlobals");
65 }
66 
67 // =============================================================================
68 // XGlobals
69 // =============================================================================
70 uno::Reference<excel::XApplication >
getApplication()71 ScVbaGlobals::getApplication() throw (uno::RuntimeException)
72 {
73 //	OSL_TRACE("In ScVbaGlobals::getApplication");
74         if ( !mxApplication.is() )
75 	    mxApplication.set( new ScVbaApplication( mxContext) );
76    	return mxApplication;
77 }
78 
79 
80 uno::Reference<excel::XApplication > SAL_CALL
getExcel()81 ScVbaGlobals::getExcel() throw (uno::RuntimeException)
82 {
83    	return getApplication();
84 }
85 
86 
87 
88 uno::Reference< excel::XWorkbook > SAL_CALL
getActiveWorkbook()89 ScVbaGlobals::getActiveWorkbook() throw (uno::RuntimeException)
90 {
91 //	OSL_TRACE("In ScVbaGlobals::getActiveWorkbook");
92     uno::Reference< excel::XWorkbook > xWorkbook( getApplication()->getActiveWorkbook(), uno::UNO_QUERY);
93     if ( xWorkbook.is() )
94     {
95         return xWorkbook;
96     }
97 // FIXME check if this is correct/desired behavior
98     throw uno::RuntimeException( rtl::OUString::createFromAscii(
99         "No activeWorkbook available" ), Reference< uno::XInterface >() );
100 }
101 
102 
103 uno::Reference< excel::XWindow > SAL_CALL
getActiveWindow()104 ScVbaGlobals::getActiveWindow() throw (uno::RuntimeException)
105 {
106     return getApplication()->getActiveWindow();
107 }
108 
109 uno::Reference< excel::XWorksheet > SAL_CALL
getActiveSheet()110 ScVbaGlobals::getActiveSheet() throw (uno::RuntimeException)
111 {
112     return getApplication()->getActiveSheet();
113 }
114 
115 uno::Any SAL_CALL
WorkBooks(const uno::Any & aIndex)116 ScVbaGlobals::WorkBooks( const uno::Any& aIndex ) throw (uno::RuntimeException)
117 {
118 	return uno::Any( getApplication()->Workbooks(aIndex) );
119 }
120 
121 uno::Any SAL_CALL
WorkSheets(const uno::Any & aIndex)122 ScVbaGlobals::WorkSheets(const uno::Any& aIndex) throw (uno::RuntimeException)
123 {
124 	return getApplication()->Worksheets( aIndex );
125 }
126 uno::Any SAL_CALL
Sheets(const uno::Any & aIndex)127 ScVbaGlobals::Sheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
128 {
129 	return WorkSheets( aIndex );
130 }
131 
132 uno::Any SAL_CALL
Range(const uno::Any & Cell1,const uno::Any & Cell2)133 ScVbaGlobals::Range( const uno::Any& Cell1, const uno::Any& Cell2 ) throw (uno::RuntimeException)
134 {
135 	return getApplication()->Range( Cell1, Cell2 );
136 }
137 
138 uno::Any SAL_CALL
Names(const css::uno::Any & aIndex)139 ScVbaGlobals::Names( const css::uno::Any& aIndex ) throw ( uno::RuntimeException )
140 {
141 	return getApplication()->Names( aIndex );
142 }
143 
144 uno::Reference< excel::XRange > SAL_CALL
getActiveCell()145 ScVbaGlobals::getActiveCell() throw (uno::RuntimeException)
146 {
147 	return getApplication()->getActiveCell();
148 }
149 
150 uno::Reference< XAssistant > SAL_CALL
getAssistant()151 ScVbaGlobals::getAssistant() throw (uno::RuntimeException)
152 {
153 	return getApplication()->getAssistant();
154 }
155 
156 uno::Any SAL_CALL
getSelection()157 ScVbaGlobals::getSelection() throw (uno::RuntimeException)
158 {
159 	return getApplication()->getSelection();
160 }
161 
162 uno::Reference< excel::XWorkbook > SAL_CALL
getThisWorkbook()163 ScVbaGlobals::getThisWorkbook() throw (uno::RuntimeException)
164 {
165 	return getApplication()->getThisWorkbook();
166 }
167 void SAL_CALL
Calculate()168 ScVbaGlobals::Calculate()  throw (::com::sun::star::script::BasicErrorException, ::com::sun::star::uno::RuntimeException)
169 {
170 	return getApplication()->Calculate();
171 }
172 
173 uno::Reference< excel::XRange > SAL_CALL
Cells(const uno::Any & RowIndex,const uno::Any & ColumnIndex)174 ScVbaGlobals::Cells( const uno::Any& RowIndex, const uno::Any& ColumnIndex ) throw (uno::RuntimeException)
175 {
176 	return getApplication()->getActiveSheet()->Cells( RowIndex, ColumnIndex );
177 }
178 uno::Reference< excel::XRange > SAL_CALL
Columns(const uno::Any & aIndex)179 ScVbaGlobals::Columns( const uno::Any& aIndex ) throw (uno::RuntimeException)
180 {
181 	return getApplication()->getActiveSheet()->Columns( aIndex );
182 }
183 
184 uno::Any SAL_CALL
CommandBars(const uno::Any & aIndex)185 ScVbaGlobals::CommandBars( const uno::Any& aIndex ) throw (uno::RuntimeException)
186 {
187 	uno::Reference< XApplicationBase > xBase( getApplication(), uno::UNO_QUERY_THROW );
188 	return xBase->CommandBars( aIndex );
189 }
190 
191 css::uno::Reference< ov::excel::XRange > SAL_CALL
Union(const css::uno::Reference<ov::excel::XRange> & Arg1,const css::uno::Reference<ov::excel::XRange> & Arg2,const css::uno::Any & Arg3,const css::uno::Any & Arg4,const css::uno::Any & Arg5,const css::uno::Any & Arg6,const css::uno::Any & Arg7,const css::uno::Any & Arg8,const css::uno::Any & Arg9,const css::uno::Any & Arg10,const css::uno::Any & Arg11,const css::uno::Any & Arg12,const css::uno::Any & Arg13,const css::uno::Any & Arg14,const css::uno::Any & Arg15,const css::uno::Any & Arg16,const css::uno::Any & Arg17,const css::uno::Any & Arg18,const css::uno::Any & Arg19,const css::uno::Any & Arg20,const css::uno::Any & Arg21,const css::uno::Any & Arg22,const css::uno::Any & Arg23,const css::uno::Any & Arg24,const css::uno::Any & Arg25,const css::uno::Any & Arg26,const css::uno::Any & Arg27,const css::uno::Any & Arg28,const css::uno::Any & Arg29,const css::uno::Any & Arg30)192 ScVbaGlobals::Union( const css::uno::Reference< ov::excel::XRange >& Arg1, const css::uno::Reference< ov::excel::XRange >& Arg2, const css::uno::Any& Arg3, const css::uno::Any& Arg4, const css::uno::Any& Arg5, const css::uno::Any& Arg6, const css::uno::Any& Arg7, const css::uno::Any& Arg8, const css::uno::Any& Arg9, const css::uno::Any& Arg10, const css::uno::Any& Arg11, const css::uno::Any& Arg12, const css::uno::Any& Arg13, const css::uno::Any& Arg14, const css::uno::Any& Arg15, const css::uno::Any& Arg16, const css::uno::Any& Arg17, const css::uno::Any& Arg18, const css::uno::Any& Arg19, const css::uno::Any& Arg20, const css::uno::Any& Arg21, const css::uno::Any& Arg22, const css::uno::Any& Arg23, const css::uno::Any& Arg24, const css::uno::Any& Arg25, const css::uno::Any& Arg26, const css::uno::Any& Arg27, const css::uno::Any& Arg28, const css::uno::Any& Arg29, const css::uno::Any& Arg30 ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
193 {
194 	return getApplication()->Union(  Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30 );
195 }
196 css::uno::Reference< ov::excel::XRange > SAL_CALL
Intersect(const css::uno::Reference<ov::excel::XRange> & Arg1,const css::uno::Reference<ov::excel::XRange> & Arg2,const css::uno::Any & Arg3,const css::uno::Any & Arg4,const css::uno::Any & Arg5,const css::uno::Any & Arg6,const css::uno::Any & Arg7,const css::uno::Any & Arg8,const css::uno::Any & Arg9,const css::uno::Any & Arg10,const css::uno::Any & Arg11,const css::uno::Any & Arg12,const css::uno::Any & Arg13,const css::uno::Any & Arg14,const css::uno::Any & Arg15,const css::uno::Any & Arg16,const css::uno::Any & Arg17,const css::uno::Any & Arg18,const css::uno::Any & Arg19,const css::uno::Any & Arg20,const css::uno::Any & Arg21,const css::uno::Any & Arg22,const css::uno::Any & Arg23,const css::uno::Any & Arg24,const css::uno::Any & Arg25,const css::uno::Any & Arg26,const css::uno::Any & Arg27,const css::uno::Any & Arg28,const css::uno::Any & Arg29,const css::uno::Any & Arg30)197 ScVbaGlobals::Intersect( const css::uno::Reference< ov::excel::XRange >& Arg1, const css::uno::Reference< ov::excel::XRange >& Arg2, const css::uno::Any& Arg3, const css::uno::Any& Arg4, const css::uno::Any& Arg5, const css::uno::Any& Arg6, const css::uno::Any& Arg7, const css::uno::Any& Arg8, const css::uno::Any& Arg9, const css::uno::Any& Arg10, const css::uno::Any& Arg11, const css::uno::Any& Arg12, const css::uno::Any& Arg13, const css::uno::Any& Arg14, const css::uno::Any& Arg15, const css::uno::Any& Arg16, const css::uno::Any& Arg17, const css::uno::Any& Arg18, const css::uno::Any& Arg19, const css::uno::Any& Arg20, const css::uno::Any& Arg21, const css::uno::Any& Arg22, const css::uno::Any& Arg23, const css::uno::Any& Arg24, const css::uno::Any& Arg25, const css::uno::Any& Arg26, const css::uno::Any& Arg27, const css::uno::Any& Arg28, const css::uno::Any& Arg29, const css::uno::Any& Arg30 ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
198 {
199 	return getApplication()->Intersect(  Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30 );
200 }
201 
202 uno::Any SAL_CALL
Evaluate(const::rtl::OUString & Name)203 ScVbaGlobals::Evaluate( const ::rtl::OUString& Name ) throw (uno::RuntimeException)
204 {
205 	return getApplication()->Evaluate( Name );
206 }
207 
208 css::uno::Any SAL_CALL
WorksheetFunction()209 ScVbaGlobals::WorksheetFunction(  ) throw (css::uno::RuntimeException)
210 {
211 	return getApplication()->WorksheetFunction();
212 }
213 
214 uno::Any SAL_CALL
Windows(const uno::Any & aIndex)215 ScVbaGlobals::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException)
216 {
217 	return getApplication()->Windows( aIndex );
218 }
219 
220 uno::Reference< excel::XRange > SAL_CALL
Rows(const uno::Any & aIndex)221 ScVbaGlobals::Rows( const uno::Any& aIndex ) throw (uno::RuntimeException)
222 {
223 	return getApplication()->getActiveSheet()->Rows( aIndex );
224 
225 }
226 
227 
228 uno::Any SAL_CALL
getDebug()229 ScVbaGlobals::getDebug() throw (uno::RuntimeException)
230 {
231 	try // return empty object on error
232 	{
233 		uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
234 		uno::Reference< uno::XInterface > xVBADebug = xServiceManager->createInstanceWithContext(
235             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.Debug" ) ), mxContext );
236         return uno::Any( xVBADebug );
237 	}
238     catch( uno::Exception& )
239 	{
240 	}
241 	return uno::Any();
242 }
243 
244 uno::Sequence< ::rtl::OUString > SAL_CALL
getAvailableServiceNames()245 ScVbaGlobals::getAvailableServiceNames(  ) throw (uno::RuntimeException)
246 {
247     static bool bInit = false;
248     static uno::Sequence< rtl::OUString > serviceNames( ScVbaGlobals_BASE::getAvailableServiceNames() );
249     if ( !bInit )
250     {
251          rtl::OUString names[] = {
252             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Range" ) ),
253             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Workbook" ) ),
254             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Window" ) ),
255             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Worksheet" ) ),
256             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Application" ) ),
257             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "ooo.vba.excel.Hyperlink" ) ),
258             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.script.vba.VBASpreadsheetEventProcessor" ) )
259           };
260         sal_Int32 nExcelServices = ( sizeof( names )/ sizeof( names[0] ) );
261         sal_Int32 startIndex = serviceNames.getLength();
262         serviceNames.realloc( serviceNames.getLength() + nExcelServices );
263         for ( sal_Int32 index = 0; index < nExcelServices; ++index )
264              serviceNames[ startIndex + index ] = names[ index ];
265         bInit = true;
266     }
267     return serviceNames;
268 }
269 
270 rtl::OUString&
getServiceImplName()271 ScVbaGlobals::getServiceImplName()
272 {
273         static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaGlobals") );
274         return sImplName;
275 }
276 
277 uno::Sequence< rtl::OUString >
getServiceNames()278 ScVbaGlobals::getServiceNames()
279 {
280         static uno::Sequence< rtl::OUString > aServiceNames;
281         if ( aServiceNames.getLength() == 0 )
282         {
283                 aServiceNames.realloc( 1 );
284                 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Globals" ) );
285         }
286         return aServiceNames;
287 }
288 
289 namespace globals
290 {
291 namespace sdecl = comphelper::service_decl;
292 sdecl::vba_service_class_<ScVbaGlobals, sdecl::with_args<true> > serviceImpl;
293 extern sdecl::ServiceDecl const serviceDecl(
294     serviceImpl,
295     "ScVbaGlobals",
296     "ooo.vba.excel.Globals" );
297 }
298 
299