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 "vbaglobals.hxx"
20 
21 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
22 #include <comphelper/sequence.hxx>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/frame/XModel.hpp>
25 
26 #include "vbaapplication.hxx"
27 
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
30 using namespace ::ooo::vba;
31 
32 // ScVbaGlobals
33 
34 //ScVbaGlobals::ScVbaGlobals(  css::uno::Reference< css::uno::XComponentContext >const& rxContext,  ) : ScVbaGlobals_BASE( uno::Reference< XHelperInterface >(), rxContext )
35 
ScVbaGlobals(uno::Sequence<uno::Any> const & aArgs,uno::Reference<uno::XComponentContext> const & rxContext)36 ScVbaGlobals::ScVbaGlobals( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& rxContext ) : ScVbaGlobals_BASE( uno::Reference< XHelperInterface >(), rxContext, "ExcelDocumentContext" )
37 {
38     uno::Sequence< beans::PropertyValue > aInitArgs( aArgs.getLength() + 1 );
39     aInitArgs[ 0 ].Name = "Application";
40     aInitArgs[ 0 ].Value <<= getApplication();
41     if ( aArgs.hasElements() )
42     {
43         aInitArgs[ 1 ].Name = "ExcelDocumentContext";
44         aInitArgs[ 1 ].Value <<= getXSomethingFromArgs< frame::XModel >( aArgs, 0 );
45     }
46     init( aInitArgs );
47 }
48 
~ScVbaGlobals()49 ScVbaGlobals::~ScVbaGlobals()
50 {
51 }
52 
53 // XGlobals
54 
55 uno::Reference<excel::XApplication > const &
getApplication()56 ScVbaGlobals::getApplication()
57 {
58     if ( !mxApplication.is() )
59         mxApplication.set( new ScVbaApplication( mxContext) );
60     return mxApplication;
61 }
62 
63 uno::Reference<excel::XApplication > SAL_CALL
getExcel()64 ScVbaGlobals::getExcel()
65 {
66        return getApplication();
67 }
68 
69 uno::Reference< excel::XWorkbook > SAL_CALL
getActiveWorkbook()70 ScVbaGlobals::getActiveWorkbook()
71 {
72     uno::Reference< excel::XWorkbook > xWorkbook( getApplication()->getActiveWorkbook(), uno::UNO_SET_THROW);
73     return xWorkbook;
74 }
75 
76 uno::Reference< excel::XWindow > SAL_CALL
getActiveWindow()77 ScVbaGlobals::getActiveWindow()
78 {
79     return getApplication()->getActiveWindow();
80 }
81 
82 uno::Reference< excel::XWorksheet > SAL_CALL
getActiveSheet()83 ScVbaGlobals::getActiveSheet()
84 {
85     return getApplication()->getActiveSheet();
86 }
87 
88 uno::Any SAL_CALL
WorkBooks(const uno::Any & aIndex)89 ScVbaGlobals::WorkBooks( const uno::Any& aIndex )
90 {
91     return getApplication()->Workbooks(aIndex);
92 }
93 
94 uno::Any SAL_CALL
WorkSheets(const uno::Any & aIndex)95 ScVbaGlobals::WorkSheets(const uno::Any& aIndex)
96 {
97     return getApplication()->Worksheets( aIndex );
98 }
99 uno::Any SAL_CALL
Sheets(const uno::Any & aIndex)100 ScVbaGlobals::Sheets( const uno::Any& aIndex )
101 {
102     return WorkSheets( aIndex );
103 }
104 
105 uno::Any SAL_CALL
Range(const uno::Any & Cell1,const uno::Any & Cell2)106 ScVbaGlobals::Range( const uno::Any& Cell1, const uno::Any& Cell2 )
107 {
108     return getApplication()->Range( Cell1, Cell2 );
109 }
110 
111 uno::Any SAL_CALL
Names(const css::uno::Any & aIndex)112 ScVbaGlobals::Names( const css::uno::Any& aIndex )
113 {
114     return getApplication()->Names( aIndex );
115 }
116 
117 uno::Reference< excel::XRange > SAL_CALL
getActiveCell()118 ScVbaGlobals::getActiveCell()
119 {
120     return getApplication()->getActiveCell();
121 }
122 
123 uno::Reference< XAssistant > SAL_CALL
getAssistant()124 ScVbaGlobals::getAssistant()
125 {
126     return getApplication()->getAssistant();
127 }
128 
129 uno::Any SAL_CALL
getSelection()130 ScVbaGlobals::getSelection()
131 {
132     return getApplication()->getSelection();
133 }
134 
135 uno::Reference< excel::XWorkbook > SAL_CALL
getThisWorkbook()136 ScVbaGlobals::getThisWorkbook()
137 {
138     return getApplication()->getThisWorkbook();
139 }
140 void SAL_CALL
Calculate()141 ScVbaGlobals::Calculate()
142 {
143     return getApplication()->Calculate();
144 }
145 
146 uno::Reference< excel::XRange > SAL_CALL
Cells(const uno::Any & RowIndex,const uno::Any & ColumnIndex)147 ScVbaGlobals::Cells( const uno::Any& RowIndex, const uno::Any& ColumnIndex )
148 {
149     return getApplication()->getActiveSheet()->Cells( RowIndex, ColumnIndex );
150 }
151 uno::Reference< excel::XRange > SAL_CALL
Columns(const uno::Any & aIndex)152 ScVbaGlobals::Columns( const uno::Any& aIndex )
153 {
154     return getApplication()->getActiveSheet()->Columns( aIndex );
155 }
156 
157 uno::Any SAL_CALL
CommandBars(const uno::Any & aIndex)158 ScVbaGlobals::CommandBars( const uno::Any& aIndex )
159 {
160     uno::Reference< XApplicationBase > xBase( getApplication(), uno::UNO_QUERY_THROW );
161     return xBase->CommandBars( aIndex );
162 }
163 
164 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)165 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 )
166 {
167     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 );
168 }
169 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)170 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 )
171 {
172     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 );
173 }
174 
175 uno::Any SAL_CALL
Evaluate(const OUString & Name)176 ScVbaGlobals::Evaluate( const OUString& Name )
177 {
178     return getApplication()->Evaluate( Name );
179 }
180 
181 css::uno::Any SAL_CALL
WorksheetFunction()182 ScVbaGlobals::WorksheetFunction(  )
183 {
184     return getApplication()->WorksheetFunction();
185 }
186 
187 uno::Any SAL_CALL
Windows(const uno::Any & aIndex)188 ScVbaGlobals::Windows( const uno::Any& aIndex )
189 {
190     return getApplication()->Windows( aIndex );
191 }
192 
193 uno::Reference< excel::XRange > SAL_CALL
Rows(const uno::Any & aIndex)194 ScVbaGlobals::Rows( const uno::Any& aIndex )
195 {
196     return getApplication()->getActiveSheet()->Rows( aIndex );
197 
198 }
199 
200 uno::Any SAL_CALL
getDebug()201 ScVbaGlobals::getDebug()
202 {
203     try // return empty object on error
204     {
205         uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
206         uno::Reference< uno::XInterface > xVBADebug = xServiceManager->createInstanceWithContext(
207             "ooo.vba.Debug", mxContext );
208         return uno::Any( xVBADebug );
209     }
210     catch( uno::Exception& )
211     {
212     }
213     return uno::Any();
214 }
215 
216 uno::Any SAL_CALL
MenuBars(const uno::Any & aIndex)217 ScVbaGlobals::MenuBars( const uno::Any& aIndex )
218 {
219     return getApplication()->MenuBars(aIndex);
220 }
221 
222 uno::Sequence< OUString > SAL_CALL
getAvailableServiceNames()223 ScVbaGlobals::getAvailableServiceNames(  )
224 {
225     static const uno::Sequence< OUString > serviceNames = comphelper::concatSequences(
226         ScVbaGlobals_BASE::getAvailableServiceNames(),
227         uno::Sequence< OUString >
228         {
229             "ooo.vba.excel.Range",
230             "ooo.vba.excel.Workbook",
231             "ooo.vba.excel.Window",
232             "ooo.vba.excel.Worksheet",
233             "ooo.vba.excel.Application",
234             "ooo.vba.excel.Hyperlink",
235             "com.sun.star.script.vba.VBASpreadsheetEventProcessor"
236         } );
237     return serviceNames;
238 }
239 
240 OUString
getServiceImplName()241 ScVbaGlobals::getServiceImplName()
242 {
243     return "ScVbaGlobals";
244 }
245 
246 uno::Sequence< OUString >
getServiceNames()247 ScVbaGlobals::getServiceNames()
248 {
249         static uno::Sequence< OUString > aServiceNames
250         {
251             "ooo.vba.excel.Globals"
252         };
253         return aServiceNames;
254 }
255 
256 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
ScVbaGlobals_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const & arguments)257 ScVbaGlobals_get_implementation(
258     css::uno::XComponentContext *context,
259     css::uno::Sequence<css::uno::Any> const &arguments)
260 {
261     return cppu::acquire(new ScVbaGlobals(arguments, context));
262 }
263 
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
265