xref: /aoo41x/main/sc/source/ui/vba/vbaapplication.cxx (revision b3f79822)
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 
24 #include <stdio.h>
25 
26 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
27 #include <com/sun/star/sheet/XSpreadsheets.hpp>
28 #include <com/sun/star/view/XSelectionSupplier.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <ooo/vba/excel/XlCalculation.hpp>
31 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
32 #include <com/sun/star/sheet/XCalculatable.hpp>
33 #include <com/sun/star/frame/XLayoutManager.hpp>
34 #include <com/sun/star/task/XStatusIndicatorSupplier.hpp>
35 #include <com/sun/star/task/XStatusIndicator.hpp>
36 #include <ooo/vba/excel/XlMousePointer.hpp>
37 #include <com/sun/star/sheet/XNamedRanges.hpp>
38 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
39 #include <ooo/vba/XExecutableDialog.hpp>
40 
41 #include "vbaapplication.hxx"
42 #include "vbaworkbooks.hxx"
43 #include "vbaworkbook.hxx"
44 #include "vbaworksheets.hxx"
45 #include "vbarange.hxx"
46 #include "vbawsfunction.hxx"
47 #include "vbadialogs.hxx"
48 #include "vbawindow.hxx"
49 #include "vbawindows.hxx"
50 #include "vbaglobals.hxx"
51 #include "tabvwsh.hxx"
52 #include "gridwin.hxx"
53 #include "vbanames.hxx"
54 #include <vbahelper/vbashape.hxx>
55 #include "vbatextboxshape.hxx"
56 #include "vbaassistant.hxx"
57 #include "sc.hrc"
58 
59 #include <osl/file.hxx>
60 #include <rtl/instance.hxx>
61 
62 #include <sfx2/request.hxx>
63 #include <sfx2/objsh.hxx>
64 #include <sfx2/viewfrm.hxx>
65 #include <sfx2/app.hxx>
66 
67 #include <toolkit/awt/vclxwindow.hxx>
68 #include <toolkit/helper/vclunohelper.hxx>
69 
70 #include <tools/diagnose_ex.h>
71 
72 #include <docuno.hxx>
73 
74 #include <basic/sbx.hxx>
75 #include <basic/sbstar.hxx>
76 #include <basic/sbuno.hxx>
77 #include <basic/sbmeth.hxx>
78 
79 #include "convuno.hxx"
80 #include "cellsuno.hxx"
81 #include "docsh.hxx"
82 #include <vbahelper/helperdecl.hxx>
83 #include "excelvbahelper.hxx"
84 
85 
86 using namespace ::ooo::vba;
87 using namespace ::com::sun::star;
88 
89 // #TODO is this defined somewhere else?
90 #if ( defined UNX ) || ( defined OS2 ) //unix
91 #define FILE_PATH_SEPERATOR "/"
92 #else // windows
93 #define FILE_PATH_SEPERATOR "\\"
94 #endif
95 
96 uno::Any sbxToUnoValue( SbxVariable* pVar );
97 
98 // ============================================================================
99 
100 /** Global application settings shared by all open workbooks. */
101 struct ScVbaAppSettings
102 {
103     sal_Int32 mnCalculation;
104     sal_Bool mbDisplayAlerts;
105     sal_Bool mbEnableEvents;
106 
107     explicit ScVbaAppSettings();
108 };
109 
ScVbaAppSettings()110 ScVbaAppSettings::ScVbaAppSettings() :
111     mnCalculation( excel::XlCalculation::xlCalculationAutomatic ),
112     mbDisplayAlerts( sal_True ),
113     mbEnableEvents( sal_True )
114 {
115 }
116 
117 struct ScVbaStaticAppSettings : public ::rtl::Static< ScVbaAppSettings, ScVbaStaticAppSettings > {};
118 
119 // ============================================================================
120 
ScVbaApplication(const uno::Reference<uno::XComponentContext> & xContext)121 ScVbaApplication::ScVbaApplication( const uno::Reference<uno::XComponentContext >& xContext ) :
122     ScVbaApplication_BASE( xContext ),
123     mrAppSettings( ScVbaStaticAppSettings::get() )
124 {
125 }
126 
~ScVbaApplication()127 ScVbaApplication::~ScVbaApplication()
128 {
129 }
130 
getDocumentEventsEnabled()131 /*static*/ bool ScVbaApplication::getDocumentEventsEnabled()
132 {
133     return ScVbaStaticAppSettings::get().mbEnableEvents;
134 }
135 
GetDocShell(const uno::Reference<frame::XModel> & xModel)136 SfxObjectShell* ScVbaApplication::GetDocShell( const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException)
137 {
138     return static_cast< SfxObjectShell* >( excel::getDocShell( xModel ) );
139 }
140 
141 ::rtl::OUString SAL_CALL
getExactName(const::rtl::OUString & aApproximateName)142 ScVbaApplication::getExactName( const ::rtl::OUString& aApproximateName ) throw (uno::RuntimeException)
143 {
144     uno::Reference< beans::XExactName > xWSF( new ScVbaWSFunction( this, mxContext ) );
145     return xWSF->getExactName( aApproximateName );
146 }
147 
148 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
getIntrospection()149 ScVbaApplication::getIntrospection() throw(css::uno::RuntimeException)
150 {
151     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
152     return xWSF->getIntrospection();
153 }
154 
155 uno::Any SAL_CALL
invoke(const::rtl::OUString & FunctionName,const uno::Sequence<uno::Any> & Params,uno::Sequence<sal_Int16> & OutParamIndex,uno::Sequence<uno::Any> & OutParam)156 ScVbaApplication::invoke( const ::rtl::OUString& FunctionName, const uno::Sequence< uno::Any >& Params, uno::Sequence< sal_Int16 >& OutParamIndex, uno::Sequence< uno::Any >& OutParam) throw(lang::IllegalArgumentException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
157 {
158     /*  When calling the functions directly at the Application object, no runtime
159         errors are thrown, but the error is inserted into the return value. */
160     uno::Any aAny;
161     try
162     {
163         uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
164         aAny = xWSF->invoke( FunctionName, Params, OutParamIndex, OutParam );
165     }
166     catch( uno::Exception& )
167     {
168         aAny <<= script::BasicErrorException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), 1000, ::rtl::OUString() );
169     }
170     return aAny;
171 }
172 
173 void SAL_CALL
setValue(const::rtl::OUString & PropertyName,const uno::Any & Value)174 ScVbaApplication::setValue( const ::rtl::OUString& PropertyName, const uno::Any& Value ) throw(beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
175 {
176     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
177     xWSF->setValue( PropertyName, Value );
178 }
179 
180 uno::Any SAL_CALL
getValue(const::rtl::OUString & PropertyName)181 ScVbaApplication::getValue( const ::rtl::OUString& PropertyName ) throw(beans::UnknownPropertyException, uno::RuntimeException)
182 {
183     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
184     return xWSF->getValue( PropertyName );
185 }
186 
187 sal_Bool SAL_CALL
hasMethod(const::rtl::OUString & Name)188 ScVbaApplication::hasMethod( const ::rtl::OUString& Name ) throw(uno::RuntimeException)
189 {
190     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
191     return xWSF->hasMethod( Name );
192 }
193 
194 sal_Bool SAL_CALL
hasProperty(const::rtl::OUString & Name)195 ScVbaApplication::hasProperty( const ::rtl::OUString& Name ) throw(uno::RuntimeException)
196 {
197     uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) );
198     return xWSF->hasProperty( Name );
199 }
200 
201 uno::Reference< excel::XWorkbook >
getActiveWorkbook()202 ScVbaApplication::getActiveWorkbook() throw (uno::RuntimeException)
203 {
204 	uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
205     uno::Reference< excel::XWorkbook > xWorkbook( getVBADocument( xModel ), uno::UNO_QUERY );
206     if( xWorkbook.is() ) return xWorkbook;
207     // #i116936# getVBADocument() may return null in documents without global VBA mode enabled
208     return new ScVbaWorkbook( this, mxContext, xModel );
209 }
210 
211 uno::Reference< excel::XWorkbook > SAL_CALL
getThisWorkbook()212 ScVbaApplication::getThisWorkbook() throw (uno::RuntimeException)
213 {
214 	uno::Reference< frame::XModel > xModel( getThisExcelDoc( mxContext ), uno::UNO_SET_THROW );
215     uno::Reference< excel::XWorkbook > xWorkbook( getVBADocument( xModel ), uno::UNO_QUERY );
216     if( xWorkbook.is() ) return xWorkbook;
217     // #i116936# getVBADocument() may return null in documents without global VBA mode enabled
218     return new ScVbaWorkbook( this, mxContext, xModel );
219 }
220 
221 uno::Reference< XAssistant > SAL_CALL
getAssistant()222 ScVbaApplication::getAssistant() throw (uno::RuntimeException)
223 {
224     return uno::Reference< XAssistant >( new ScVbaAssistant( this, mxContext ) );
225 }
226 
227 uno::Any SAL_CALL
getSelection()228 ScVbaApplication::getSelection() throw (uno::RuntimeException)
229 {
230     OSL_TRACE("** ScVbaApplication::getSelection() ** ");
231     uno::Reference< frame::XModel > xModel( getCurrentDocument() );
232     uno::Reference< lang::XServiceInfo > xServiceInfo( xModel->getCurrentSelection(), uno::UNO_QUERY_THROW );
233     rtl::OUString sImpementaionName = xServiceInfo->getImplementationName();
234     if( sImpementaionName.equalsIgnoreAsciiCaseAscii("com.sun.star.drawing.SvxShapeCollection") )
235     {
236         uno::Reference< drawing::XShapes > xShapes( xModel->getCurrentSelection(), uno::UNO_QUERY_THROW );
237         uno::Reference< container::XIndexAccess > xIndexAccess( xShapes, uno::UNO_QUERY_THROW );
238         uno::Reference< drawing::XShape > xShape( xIndexAccess->getByIndex(0), uno::UNO_QUERY_THROW );
239 	// if ScVbaShape::getType( xShape ) == office::MsoShapeType::msoAutoShape
240 	// and the uno object implements the com.sun.star.drawing.Text service
241 	// return a textboxshape object
242 	if ( ScVbaShape::getType( xShape ) == office::MsoShapeType::msoAutoShape )
243 	{
244 		uno::Reference< lang::XServiceInfo > xShapeServiceInfo( xShape, uno::UNO_QUERY_THROW );
245 		if ( xShapeServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.Text" ) ) )  )
246 		{
247                 return uno::makeAny( uno::Reference< msforms::XTextBoxShape >(new ScVbaTextBoxShape( mxContext, xShape, xShapes, xModel ) ) );
248 		}
249 	}
250         return uno::makeAny( uno::Reference< msforms::XShape >(new ScVbaShape( this, mxContext, xShape, xShapes, xModel, ScVbaShape::getType( xShape ) ) ) );
251     }
252     else if( xServiceInfo->supportsService( rtl::OUString::createFromAscii("com.sun.star.sheet.SheetCellRange")) ||
253              xServiceInfo->supportsService( rtl::OUString::createFromAscii("com.sun.star.sheet.SheetCellRanges")))
254     {
255 	    uno::Reference< table::XCellRange > xRange( getCurrentDocument()->getCurrentSelection(), ::uno::UNO_QUERY);
256 	    if ( !xRange.is() )
257 	    {
258 		    uno::Reference< sheet::XSheetCellRangeContainer > xRanges( getCurrentDocument()->getCurrentSelection(), ::uno::UNO_QUERY);
259 		    if ( xRanges.is() )
260                 return uno::makeAny( uno::Reference< excel::XRange >( new ScVbaRange( excel::getUnoSheetModuleObj( xRanges ), mxContext, xRanges ) ) );
261 
262 	    }
263         return uno::makeAny( uno::Reference< excel::XRange >(new ScVbaRange( excel::getUnoSheetModuleObj( xRange ), mxContext, xRange ) ) );
264     }
265     else
266     {
267         throw uno::RuntimeException( sImpementaionName + rtl::OUString::createFromAscii(" not suported"), uno::Reference< uno::XInterface >() );
268     }
269 }
270 
271 uno::Reference< excel::XRange >
getActiveCell()272 ScVbaApplication::getActiveCell() throw (uno::RuntimeException )
273 {
274 	uno::Reference< sheet::XSpreadsheetView > xView( getCurrentDocument()->getCurrentController(), uno::UNO_QUERY_THROW );
275 	uno::Reference< table::XCellRange > xRange( xView->getActiveSheet(), ::uno::UNO_QUERY_THROW);
276 	ScTabViewShell* pViewShell = excel::getCurrentBestViewShell(mxContext);
277 	if ( !pViewShell )
278 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewShell available"), uno::Reference< uno::XInterface >() );
279 	ScViewData* pTabView = pViewShell->GetViewData();
280 	if ( !pTabView )
281 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewData available"), uno::Reference< uno::XInterface >() );
282 
283 	sal_Int32 nCursorX = pTabView->GetCurX();
284 	sal_Int32 nCursorY = pTabView->GetCurY();
285 
286     // #i117392# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
287 	return new ScVbaRange( excel::getUnoSheetModuleObj( xRange ), mxContext, xRange->getCellRangeByPosition( nCursorX, nCursorY, nCursorX, nCursorY ) );
288 }
289 
290 uno::Any SAL_CALL
Workbooks(const uno::Any & aIndex)291 ScVbaApplication::Workbooks( const uno::Any& aIndex ) throw (uno::RuntimeException)
292 {
293 	uno::Reference< XCollection > xWorkBooks( new ScVbaWorkbooks( this, mxContext ) );
294 	if (  aIndex.getValueTypeClass() == uno::TypeClass_VOID )
295 	{
296 		// void then somebody did Workbooks.something in vba
297 	    return uno::Any( xWorkBooks );
298 	}
299 
300 	return uno::Any ( xWorkBooks->Item( aIndex, uno::Any() ) );
301 }
302 
303 uno::Any SAL_CALL
Worksheets(const uno::Any & aIndex)304 ScVbaApplication::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
305 {
306     uno::Reference< excel::XWorkbook > xWorkbook( getActiveWorkbook(), uno::UNO_SET_THROW );
307     return xWorkbook->Worksheets( aIndex );
308 }
309 
310 uno::Any SAL_CALL
WorksheetFunction()311 ScVbaApplication::WorksheetFunction( ) throw (::com::sun::star::uno::RuntimeException)
312 {
313     return uno::makeAny( uno::Reference< script::XInvocation >( new ScVbaWSFunction( this, mxContext ) ) );
314 }
315 
316 uno::Any SAL_CALL
Evaluate(const::rtl::OUString & Name)317 ScVbaApplication::Evaluate( const ::rtl::OUString& Name ) throw (uno::RuntimeException)
318 {
319 	// #TODO Evaluate allows other things to be evaluated, e.g. functions
320 	// I think ( like SIN(3) etc. ) need to investigate that
321 	// named Ranges also? e.g. [MyRange] if so need a list of named ranges
322 	uno::Any aVoid;
323 	return uno::Any( getActiveWorkbook()->getActiveSheet()->Range( uno::Any( Name ), aVoid ) );
324 }
325 
326 uno::Any
Dialogs(const uno::Any & aIndex)327 ScVbaApplication::Dialogs( const uno::Any &aIndex ) throw (uno::RuntimeException)
328 {
329 	uno::Reference< excel::XDialogs > xDialogs( new ScVbaDialogs( uno::Reference< XHelperInterface >( this ), mxContext, getCurrentDocument() ) );
330 	if( !aIndex.hasValue() )
331 		return uno::Any( xDialogs );
332 	return uno::Any( xDialogs->Item( aIndex ) );
333 }
334 
335 uno::Reference< excel::XWindow > SAL_CALL
getActiveWindow()336 ScVbaApplication::getActiveWindow() throw (uno::RuntimeException)
337 {
338 	uno::Reference< frame::XModel > xModel = getCurrentDocument();
339 	uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
340 	uno::Reference< XHelperInterface > xParent( getActiveWorkbook(), uno::UNO_QUERY_THROW );
341 	uno::Reference< excel::XWindow > xWin( new ScVbaWindow( xParent, mxContext, xModel, xController ) );
342 	return xWin;
343 }
344 
345 uno::Any SAL_CALL
getCutCopyMode()346 ScVbaApplication::getCutCopyMode() throw (uno::RuntimeException)
347 {
348 	//# FIXME TODO, implementation
349 	uno::Any result;
350 	result <<= sal_False;
351 	return result;
352 }
353 
354 void SAL_CALL
setCutCopyMode(const uno::Any &)355 ScVbaApplication::setCutCopyMode( const uno::Any& /*_cutcopymode*/ ) throw (uno::RuntimeException)
356 {
357 	//# FIXME TODO, implementation
358 }
359 
360 uno::Any SAL_CALL
getStatusBar()361 ScVbaApplication::getStatusBar() throw (uno::RuntimeException)
362 {
363 	return uno::makeAny( !getDisplayStatusBar() );
364 }
365 
366 void SAL_CALL
setStatusBar(const uno::Any & _statusbar)367 ScVbaApplication::setStatusBar( const uno::Any& _statusbar ) throw (uno::RuntimeException)
368 {
369     rtl::OUString sText;
370     sal_Bool bDefault = sal_False;
371 	uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
372     uno::Reference< task::XStatusIndicatorSupplier > xStatusIndicatorSupplier( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
373     uno::Reference< task::XStatusIndicator > xStatusIndicator( xStatusIndicatorSupplier->getStatusIndicator(), uno::UNO_QUERY_THROW );
374     if( _statusbar >>= sText )
375     {
376         setDisplayStatusBar( sal_True );
377         if ( sText.getLength() )
378             xStatusIndicator->start( sText, 100 );
379         else
380             xStatusIndicator->end();        // restore normal state for empty text
381     }
382     else if( _statusbar >>= bDefault )
383     {
384         if( bDefault == sal_False )
385         {
386             xStatusIndicator->end();
387             setDisplayStatusBar( sal_True );
388         }
389     }
390     else
391         throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid prarameter. It should be a string or False" ),
392             uno::Reference< uno::XInterface >() );
393 }
394 
395 ::sal_Int32 SAL_CALL
getCalculation()396 ScVbaApplication::getCalculation() throw (uno::RuntimeException)
397 {
398     // TODO: in Excel, this is an application-wide setting
399 	uno::Reference<sheet::XCalculatable> xCalc(getCurrentDocument(), uno::UNO_QUERY_THROW);
400 	if(xCalc->isAutomaticCalculationEnabled())
401 		return excel::XlCalculation::xlCalculationAutomatic;
402 	else
403 		return excel::XlCalculation::xlCalculationManual;
404 }
405 
406 void SAL_CALL
setCalculation(::sal_Int32 _calculation)407 ScVbaApplication::setCalculation( ::sal_Int32 _calculation ) throw (uno::RuntimeException)
408 {
409     // TODO: in Excel, this is an application-wide setting
410 	uno::Reference< sheet::XCalculatable > xCalc(getCurrentDocument(), uno::UNO_QUERY_THROW);
411 	switch(_calculation)
412 	{
413 		case excel::XlCalculation::xlCalculationManual:
414 			xCalc->enableAutomaticCalculation(sal_False);
415 			break;
416 		case excel::XlCalculation::xlCalculationAutomatic:
417 		case excel::XlCalculation::xlCalculationSemiautomatic:
418 			xCalc->enableAutomaticCalculation(sal_True);
419 			break;
420 	}
421 }
422 
423 uno::Any SAL_CALL
Windows(const uno::Any & aIndex)424 ScVbaApplication::Windows( const uno::Any& aIndex  ) throw (uno::RuntimeException)
425 {
426 	uno::Reference< excel::XWindows >  xWindows( new ScVbaWindows( this, mxContext ) );
427 	if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
428 		return uno::Any( xWindows );
429 	return uno::Any( xWindows->Item( aIndex, uno::Any() ) );
430 }
431 void SAL_CALL
wait(double time)432 ScVbaApplication::wait( double time ) throw (uno::RuntimeException)
433 {
434 	StarBASIC* pBasic = SFX_APP()->GetBasic();
435 	SbxArrayRef aArgs = new SbxArray;
436 	SbxVariableRef aRef = new SbxVariable;
437 	aRef->PutDouble( time );
438 	aArgs->Put(  aRef, 1 );
439 	SbMethod* pMeth = (SbMethod*)pBasic->GetRtl()->Find( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("WaitUntil") ), SbxCLASS_METHOD );
440 
441 	if ( pMeth )
442 	{
443 		pMeth->SetParameters( aArgs );
444 		SbxVariableRef refTemp = pMeth;
445 		// forces a broadcast
446 		SbxVariableRef pNew = new  SbxMethod( *((SbxMethod*)pMeth));
447 	}
448 }
449 
450 uno::Any SAL_CALL
Range(const uno::Any & Cell1,const uno::Any & Cell2)451 ScVbaApplication::Range( const uno::Any& Cell1, const uno::Any& Cell2 ) throw (uno::RuntimeException)
452 {
453 	uno::Reference< excel::XRange > xVbRange = ScVbaRange::ApplicationRange( mxContext, Cell1, Cell2 );
454 	return uno::makeAny( xVbRange );
455 }
456 
457 uno::Any SAL_CALL
Names(const css::uno::Any & aIndex)458 ScVbaApplication::Names( const css::uno::Any& aIndex ) throw ( uno::RuntimeException )
459 {
460     uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
461     uno::Reference< beans::XPropertySet > xPropertySet( xModel, uno::UNO_QUERY_THROW );
462     uno::Reference< sheet::XNamedRanges > xNamedRanges( xPropertySet->getPropertyValue( rtl::OUString::createFromAscii("NamedRanges")) , uno::UNO_QUERY_THROW );
463     css::uno::Reference< excel::XNames > xNames ( new ScVbaNames( this , mxContext , xNamedRanges , xModel ) );
464     if (  aIndex.getValueTypeClass() == uno::TypeClass_VOID )
465     {
466         return uno::Any( xNames );
467 }
468     return uno::Any( xNames->Item( aIndex, uno::Any() ) );
469 }
470 
471 
472 uno::Reference< excel::XWorksheet > SAL_CALL
getActiveSheet()473 ScVbaApplication::getActiveSheet() throw (uno::RuntimeException)
474 {
475     uno::Reference< excel::XWorksheet > result;
476     uno::Reference< excel::XWorkbook > xWorkbook( getActiveWorkbook(), uno::UNO_QUERY );
477     if ( xWorkbook.is() )
478     {
479         uno::Reference< excel::XWorksheet > xWorksheet(
480             xWorkbook->getActiveSheet(), uno::UNO_QUERY );
481         if ( xWorksheet.is() )
482         {
483             result = xWorksheet;
484         }
485     }
486 
487     if ( !result.is() )
488     {
489         // Fixme - check if this is reasonable/desired behavior
490         throw uno::RuntimeException( rtl::OUString::createFromAscii(
491             "No activeSheet available" ), uno::Reference< uno::XInterface >() );
492     }
493     return result;
494 
495 }
496 
497 /*******************************************************************************
498  *  In msdn:
499  *  Reference   Optional Variant. The destination. Can be a Range
500  *  object, a string that contains a cell reference in R1C1-style notation,
501  *  or a string that contains a Visual Basic procedure name.
502  *  Scroll   Optional Variant. True to scrol, False to not scroll through
503  *  the window. The default is False.
504  *  Parser is split to three parts, Range, R1C1 string and procedure name.
505  *  by test excel, it seems Scroll no effect. ???
506 *******************************************************************************/
507 void SAL_CALL
GoTo(const uno::Any & Reference,const uno::Any & Scroll)508 ScVbaApplication::GoTo( const uno::Any& Reference, const uno::Any& Scroll ) throw (uno::RuntimeException)
509 {
510     //test Scroll is a boolean
511     sal_Bool bScroll = sal_False;
512     //R1C1-style string or a string of procedure name.
513 
514     if( Scroll.hasValue() )
515     {
516         sal_Bool aScroll = sal_False;
517         if( Scroll >>= aScroll )
518         {
519             bScroll = aScroll;
520         }
521         else
522             throw uno::RuntimeException( rtl::OUString::createFromAscii( "sencond parameter should be boolean" ),
523                     uno::Reference< uno::XInterface >() );
524     }
525 
526     rtl::OUString sRangeName;
527     if( Reference >>= sRangeName )
528     {
529         uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
530         uno::Reference< sheet::XSpreadsheetView > xSpreadsheet(
531                 xModel->getCurrentController(), uno::UNO_QUERY_THROW );
532         uno::Reference< sheet::XSpreadsheet > xDoc = xSpreadsheet->getActiveSheet();
533 
534         ScTabViewShell* pShell = excel::getCurrentBestViewShell( mxContext );
535         ScGridWindow* gridWindow = (ScGridWindow*)pShell->GetWindow();
536         try
537         {
538             uno::Reference< excel::XRange > xVbaSheetRange = ScVbaRange::getRangeObjectForName(
539                 mxContext, sRangeName, excel::getDocShell( xModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
540 
541             if( bScroll )
542             {
543                 xVbaSheetRange->Select();
544                 uno::Reference< excel::XWindow >  xWindow = getActiveWindow();
545                 ScSplitPos eWhich = pShell->GetViewData()->GetActivePart();
546                 sal_Int32 nValueX = pShell->GetViewData()->GetPosX(WhichH(eWhich));
547                 sal_Int32 nValueY = pShell->GetViewData()->GetPosY(WhichV(eWhich));
548                 xWindow->SmallScroll( uno::makeAny( (sal_Int16)(xVbaSheetRange->getRow() - 1) ),
549                          uno::makeAny( (sal_Int16)nValueY ),
550                          uno::makeAny( (sal_Int16)(xVbaSheetRange->getColumn() - 1)  ),
551                          uno::makeAny( (sal_Int16)nValueX ) );
552                 gridWindow->GrabFocus();
553             }
554             else
555             {
556                 xVbaSheetRange->Select();
557                 gridWindow->GrabFocus();
558             }
559         }
560         catch( uno::RuntimeException )
561         {
562             //maybe this should be a procedure name
563             //TODO for procedure name
564             //browse::XBrowseNodeFactory is a singlton. OUString::createFromAscii( "/singletons/com.sun.star.script.browse.theBrowseNodeFactory")
565             //and the createView( browse::BrowseNodeFactoryViewTypes::MACROSELECTOR ) to get a root browse::XBrowseNode.
566             //for query XInvocation interface.
567             //but how to directly get the XInvocation?
568             throw uno::RuntimeException( rtl::OUString::createFromAscii( "invalid reference for range name, it should be procedure name" ),
569                     uno::Reference< uno::XInterface >() );
570         }
571         return;
572     }
573     uno::Reference< excel::XRange > xRange;
574     if( Reference >>= xRange )
575     {
576         uno::Reference< excel::XRange > xVbaRange( Reference, uno::UNO_QUERY );
577         ScTabViewShell* pShell = excel::getCurrentBestViewShell( mxContext );
578         ScGridWindow* gridWindow = (ScGridWindow*)pShell->GetWindow();
579         if ( xVbaRange.is() )
580         {
581             //TODO bScroll should be using, In this time, it doesenot have effection
582             if( bScroll )
583             {
584                 xVbaRange->Select();
585                 uno::Reference< excel::XWindow >  xWindow = getActiveWindow();
586                 ScSplitPos eWhich = pShell->GetViewData()->GetActivePart();
587                 sal_Int32 nValueX = pShell->GetViewData()->GetPosX(WhichH(eWhich));
588                 sal_Int32 nValueY = pShell->GetViewData()->GetPosY(WhichV(eWhich));
589                 xWindow->SmallScroll( uno::makeAny( (sal_Int16)(xVbaRange->getRow() - 1) ),
590                          uno::makeAny( (sal_Int16)nValueY ),
591                          uno::makeAny( (sal_Int16)(xVbaRange->getColumn() - 1)  ),
592                          uno::makeAny( (sal_Int16)nValueX ) );
593                 gridWindow->GrabFocus();
594             }
595             else
596             {
597                 xVbaRange->Select();
598                 gridWindow->GrabFocus();
599             }
600         }
601         return;
602     }
603     throw uno::RuntimeException( rtl::OUString::createFromAscii( "invalid reference or name" ),
604             uno::Reference< uno::XInterface >() );
605 }
606 
607 sal_Int32 SAL_CALL
getCursor()608 ScVbaApplication::getCursor() throw (uno::RuntimeException)
609 {
610     sal_Int32 nPointerStyle =  getPointerStyle(getCurrentDocument());
611 
612     switch( nPointerStyle )
613     {
614         case POINTER_ARROW:
615             return excel::XlMousePointer::xlNorthwestArrow;
616         case POINTER_NULL:
617             return excel::XlMousePointer::xlDefault;
618         case POINTER_WAIT:
619             return excel::XlMousePointer::xlWait;
620         case POINTER_TEXT:
621             return excel::XlMousePointer::xlIBeam;
622         default:
623             return excel::XlMousePointer::xlDefault;
624     }
625 }
626 
627 void SAL_CALL
setCursor(sal_Int32 _cursor)628 ScVbaApplication::setCursor( sal_Int32 _cursor ) throw (uno::RuntimeException)
629 {
630     try
631     {
632 	uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
633         switch( _cursor )
634         {
635             case excel::XlMousePointer::xlNorthwestArrow:
636             {
637                 const Pointer& rPointer( POINTER_ARROW );
638                 setCursorHelper( xModel, rPointer, sal_False );
639                 break;
640             }
641             case excel::XlMousePointer::xlWait:
642             case excel::XlMousePointer::xlIBeam:
643             {
644                 const Pointer& rPointer( static_cast< PointerStyle >( _cursor ) );
645                 //It will set the edit window, toobar and statusbar's mouse pointer.
646                 setCursorHelper( xModel, rPointer, sal_True );
647                 break;
648             }
649             case excel::XlMousePointer::xlDefault:
650             {
651                 const Pointer& rPointer( POINTER_NULL );
652                 setCursorHelper( xModel, rPointer, sal_False );
653                 break;
654             }
655             default:
656                 throw uno::RuntimeException( rtl::OUString(
657                         RTL_CONSTASCII_USTRINGPARAM("Unknown value for Cursor pointer")), uno::Reference< uno::XInterface >() );
658                 // TODO: isn't this a flaw in the API? It should be allowed to throw an
659                 // IllegalArgumentException, or so
660         }
661     }
662     catch( const uno::Exception& )
663     {
664     	DBG_UNHANDLED_EXCEPTION();
665     }
666 }
667 
668 // #TODO perhaps we should switch the return type depending of the filter
669 // type, e.g. return Calc for Calc and Excel if its an imported doc
670 rtl::OUString SAL_CALL
getName()671 ScVbaApplication::getName() throw (uno::RuntimeException)
672 {
673 	static rtl::OUString appName( RTL_CONSTASCII_USTRINGPARAM("Microsoft Excel" ) );
674 	return appName;
675 }
676 
677 // #TODO #FIXME get/setDisplayAlerts are just stub impl
678 // here just the status of the switch is set
679 // the function that throws an error message needs to
680 // evaluate this switch in order to know whether it has to disable the
681 // error message thrown by OpenOffice
682 
683 void SAL_CALL
setDisplayAlerts(sal_Bool displayAlerts)684 ScVbaApplication::setDisplayAlerts(sal_Bool displayAlerts) throw (uno::RuntimeException)
685 {
686     mrAppSettings.mbDisplayAlerts = displayAlerts;
687 }
688 
689 sal_Bool SAL_CALL
getDisplayAlerts()690 ScVbaApplication::getDisplayAlerts() throw (uno::RuntimeException)
691 {
692 	return mrAppSettings.mbDisplayAlerts;
693 }
694 
695 void SAL_CALL
setEnableEvents(sal_Bool bEnable)696 ScVbaApplication::setEnableEvents(sal_Bool bEnable) throw (uno::RuntimeException)
697 {
698 	mrAppSettings.mbEnableEvents = bEnable;
699 }
700 
701 sal_Bool SAL_CALL
getEnableEvents()702 ScVbaApplication::getEnableEvents() throw (uno::RuntimeException)
703 {
704 	return mrAppSettings.mbEnableEvents;
705 }
706 
707 void SAL_CALL
Calculate()708 ScVbaApplication::Calculate() throw(  script::BasicErrorException , uno::RuntimeException )
709 {
710 	uno::Reference< frame::XModel > xModel( getCurrentDocument(), uno::UNO_QUERY_THROW );
711 	uno::Reference< sheet::XCalculatable > xCalculatable( getCurrentDocument(), uno::UNO_QUERY_THROW );
712 	xCalculatable->calculateAll();
713 }
714 
lcl_getPathSettingsService(const uno::Reference<uno::XComponentContext> & xContext)715 uno::Reference< beans::XPropertySet > lcl_getPathSettingsService( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException )
716 {
717 	static uno::Reference< beans::XPropertySet >  xPathSettings;
718 	if ( !xPathSettings.is() )
719 	{
720 		uno::Reference< lang::XMultiComponentFactory > xSMgr( xContext->getServiceManager(), uno::UNO_QUERY_THROW );
721 		xPathSettings.set( xSMgr->createInstanceWithContext(::rtl::OUString::createFromAscii("com.sun.star.util.PathSettings"), xContext), uno::UNO_QUERY_THROW );
722 	}
723 	return xPathSettings;
724 }
getOfficePath(const rtl::OUString & _sPathType)725 rtl::OUString ScVbaApplication::getOfficePath( const rtl::OUString& _sPathType ) throw ( uno::RuntimeException )
726 {
727 	rtl::OUString sRetPath;
728 	uno::Reference< beans::XPropertySet > xProps = lcl_getPathSettingsService( mxContext );
729 	try
730 	{
731 		rtl::OUString sUrl;
732 	 	xProps->getPropertyValue( _sPathType ) >>= sUrl;
733 
734 		// if its a list of paths then use the last one
735 		sal_Int32 nIndex =  sUrl.lastIndexOf( ';' ) ;
736 		if ( nIndex > 0 )
737 			sUrl = sUrl.copy( nIndex + 1 );
738 		::osl::File::getSystemPathFromFileURL( sUrl, sRetPath );
739 	}
740 	catch (uno::Exception&)
741 	{
742 		DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
743 	}
744 	return sRetPath;
745 }
746 
747 void SAL_CALL
setDefaultFilePath(const::rtl::OUString & DefaultFilePath)748 ScVbaApplication::setDefaultFilePath( const ::rtl::OUString& DefaultFilePath ) throw (uno::RuntimeException)
749 {
750 	uno::Reference< beans::XPropertySet > xProps = lcl_getPathSettingsService( mxContext );
751 	rtl::OUString aURL;
752 	osl::FileBase::getFileURLFromSystemPath( DefaultFilePath, aURL );
753 	xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Work")), uno::Any( aURL ) );
754 }
755 
756 ::rtl::OUString SAL_CALL
getDefaultFilePath()757 ScVbaApplication::getDefaultFilePath() throw (uno::RuntimeException)
758 {
759 	return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Work")));
760 }
761 
762 ::rtl::OUString SAL_CALL
getLibraryPath()763 ScVbaApplication::getLibraryPath() throw (uno::RuntimeException)
764 {
765 	return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Basic")));
766 }
767 
768 ::rtl::OUString SAL_CALL
getTemplatesPath()769 ScVbaApplication::getTemplatesPath() throw (uno::RuntimeException)
770 {
771 	return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Template")));
772 }
773 
774 ::rtl::OUString SAL_CALL
getPathSeparator()775 ScVbaApplication::getPathSeparator() throw (uno::RuntimeException)
776 {
777 	static rtl::OUString sPathSep( RTL_CONSTASCII_USTRINGPARAM( FILE_PATH_SEPERATOR ) );
778 	return sPathSep;
779 }
780 
781 // ----------------------------------------------------------------------------
782 // Helpers for Intersect and Union
783 
784 namespace {
785 
786 typedef ::std::list< ScRange > ListOfScRange;
787 
788 /** Appends all ranges of a VBA Range object in the passed Any to the list of ranges. */
lclAddToListOfScRange(ListOfScRange & rList,const uno::Any & rArg)789 void lclAddToListOfScRange( ListOfScRange& rList, const uno::Any& rArg )
790         throw (script::BasicErrorException, uno::RuntimeException)
791 {
792     if( rArg.hasValue() )
793     {
794         uno::Reference< excel::XRange > xRange( rArg, uno::UNO_QUERY_THROW );
795         uno::Reference< XCollection > xCol( xRange->Areas( uno::Any() ), uno::UNO_QUERY_THROW );
796         for( sal_Int32 nIdx = 1, nCount = xCol->getCount(); nIdx <= nCount; ++nIdx )
797         {
798             uno::Reference< excel::XRange > xAreaRange( xCol->Item( uno::Any( nIdx ), uno::Any() ), uno::UNO_QUERY_THROW );
799             uno::Reference< sheet::XCellRangeAddressable > xAddressable( xAreaRange->getCellRange(), uno::UNO_QUERY_THROW );
800             ScRange aScRange;
801             ScUnoConversion::FillScRange( aScRange, xAddressable->getRangeAddress() );
802             rList.push_back( aScRange );
803         }
804     }
805 }
806 
807 /** Returns true, if the passed ranges can be expressed by a single range. The
808     new range will be contained in r1 then, the range r2 can be removed. */
lclTryJoin(ScRange & r1,const ScRange & r2)809 bool lclTryJoin( ScRange& r1, const ScRange& r2 )
810 {
811     // 1) r2 is completely inside r1
812     if( r1.In( r2 ) )
813         return true;
814 
815     // 2) r1 is completely inside r2
816     if( r2.In( r1 ) )
817     {
818         r1 = r2;
819         return true;
820     }
821 
822     SCCOL n1L = r1.aStart.Col();
823     SCCOL n1R = r1.aEnd.Col();
824     SCROW n1T = r1.aStart.Row();
825     SCROW n1B = r1.aEnd.Row();
826     SCCOL n2L = r2.aStart.Col();
827     SCCOL n2R = r2.aEnd.Col();
828     SCROW n2T = r2.aStart.Row();
829     SCROW n2B = r2.aEnd.Row();
830 
831     // 3) r1 and r2 have equal upper and lower border
832     if( (n1T == n2T) && (n1B == n2B) )
833     {
834         // check that r1 overlaps or touches r2
835         if( ((n1L < n2L) && (n2L - 1 <= n1R)) || ((n2L < n1L) && (n1L - 1 <= n2R)) )
836         {
837             r1.aStart.SetCol( ::std::min( n1L, n2L ) );
838             r1.aEnd.SetCol( ::std::max( n1R, n2R ) );
839             return true;
840         }
841         return false;
842     }
843 
844     // 4) r1 and r2 have equal left and right border
845     if( (n1L == n2L) && (n1R == n2R) )
846     {
847         // check that r1 overlaps or touches r2
848         if( ((n1T < n2T) && (n2T + 1 <= n1B)) || ((n2T < n1T) && (n1T + 1 <= n2B)) )
849         {
850             r1.aStart.SetRow( ::std::min( n1T, n2T ) );
851             r1.aEnd.SetRow( ::std::max( n1B, n2B ) );
852             return true;
853         }
854         return false;
855     }
856 
857     // 5) cannot join these ranges
858     return false;
859 }
860 
861 /** Strips out ranges that are contained by other ranges, joins ranges that can be joined
862     together (aligned borders, e.g. A4:D10 and B4:E10 would be combined to A4:E10. */
lclJoinRanges(ListOfScRange & rList)863 void lclJoinRanges( ListOfScRange& rList )
864 {
865     ListOfScRange::iterator aOuterIt = rList.begin();
866     while( aOuterIt != rList.end() )
867     {
868         bool bAnyErased = false;    // true = any range erased from rList
869         ListOfScRange::iterator aInnerIt = rList.begin();
870         while( aInnerIt != rList.end() )
871         {
872             bool bInnerErased = false;   // true = aInnerIt erased from rList
873             // do not compare a range with itself
874             if( (aOuterIt != aInnerIt) && lclTryJoin( *aOuterIt, *aInnerIt ) )
875             {
876                 // aOuterIt points to joined range, aInnerIt will be removed
877 				aInnerIt = rList.erase( aInnerIt );
878                 bInnerErased = bAnyErased = true;
879             }
880             /*  If aInnerIt has been erased from rList, it already points to
881                 the next element (return value of list::erase()). */
882             if( !bInnerErased )
883                 ++aInnerIt;
884 		}
885         // if any range has been erased, repeat outer loop with the same range
886         if( !bAnyErased )
887             ++aOuterIt;
888 	}
889 }
890 
891 /** Intersects the passed list with all ranges of a VBA Range object in the passed Any. */
lclIntersectRanges(ListOfScRange & rList,const uno::Any & rArg)892 void lclIntersectRanges( ListOfScRange& rList, const uno::Any& rArg )
893         throw (script::BasicErrorException, uno::RuntimeException)
894 {
895     // extract the ranges from the passed argument, will throw on invalid data
896     ListOfScRange aList2;
897     lclAddToListOfScRange( aList2, rArg );
898     // do nothing, if the passed list is already empty
899     if( !rList.empty() && !aList2.empty() )
900     {
901         // save original list in a local
902         ListOfScRange aList1;
903         aList1.swap( rList );
904         // join ranges from passed argument
905         lclJoinRanges( aList2 );
906         // calculate intersection of the ranges in both lists
907         for( ListOfScRange::const_iterator aOuterIt = aList1.begin(), aOuterEnd = aList1.end(); aOuterIt != aOuterEnd; ++aOuterIt )
908         {
909             for( ListOfScRange::const_iterator aInnerIt = aList2.begin(), aInnerEnd = aList2.end(); aInnerIt != aInnerEnd; ++aInnerIt )
910             {
911                 if( aOuterIt->Intersects( *aInnerIt ) )
912                 {
913                     ScRange aIsectRange(
914                         Max( aOuterIt->aStart.Col(), aInnerIt->aStart.Col() ),
915                         Max( aOuterIt->aStart.Row(), aInnerIt->aStart.Row() ),
916                         Max( aOuterIt->aStart.Tab(), aInnerIt->aStart.Tab() ),
917                         Min( aOuterIt->aEnd.Col(),   aInnerIt->aEnd.Col() ),
918                         Min( aOuterIt->aEnd.Row(),   aInnerIt->aEnd.Row() ),
919                         Min( aOuterIt->aEnd.Tab(),   aInnerIt->aEnd.Tab() ) );
920                     rList.push_back( aIsectRange );
921                 }
922             }
923         }
924         // again, join the result ranges
925         lclJoinRanges( rList );
926     }
927 }
928 
929 /** Creates a VBA Range object from the passed list of ranges. */
lclCreateVbaRange(const uno::Reference<uno::XComponentContext> & rxContext,const uno::Reference<frame::XModel> & rxModel,const ListOfScRange & rList)930 uno::Reference< excel::XRange > lclCreateVbaRange(
931         const uno::Reference< uno::XComponentContext >& rxContext,
932         const uno::Reference< frame::XModel >& rxModel,
933         const ListOfScRange& rList ) throw (uno::RuntimeException)
934 {
935     ScDocShell* pDocShell = excel::getDocShell( rxModel );
936     if( !pDocShell ) throw uno::RuntimeException();
937 
938 	ScRangeList aCellRanges;
939 	for( ListOfScRange::const_iterator aIt = rList.begin(), aEnd = rList.end(); aIt != aEnd; ++aIt )
940 		aCellRanges.Append( *aIt );
941 
942 	if( aCellRanges.Count() == 1 )
943 	{
944         uno::Reference< table::XCellRange > xRange( new ScCellRangeObj( pDocShell, *aCellRanges.First() ) );
945 		return new ScVbaRange( excel::getUnoSheetModuleObj( xRange ), rxContext, xRange );
946 	}
947 	if( aCellRanges.Count() > 1 )
948 	{
949 		uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( pDocShell, aCellRanges ) );
950     	return new ScVbaRange( excel::getUnoSheetModuleObj( xRanges ), rxContext, xRanges );
951 	}
952 	return 0;
953 }
954 
955 } // namespace
956 
957 // ----------------------------------------------------------------------------
958 
Intersect(const uno::Reference<excel::XRange> & rArg1,const uno::Reference<excel::XRange> & rArg2,const uno::Any & rArg3,const uno::Any & rArg4,const uno::Any & rArg5,const uno::Any & rArg6,const uno::Any & rArg7,const uno::Any & rArg8,const uno::Any & rArg9,const uno::Any & rArg10,const uno::Any & rArg11,const uno::Any & rArg12,const uno::Any & rArg13,const uno::Any & rArg14,const uno::Any & rArg15,const uno::Any & rArg16,const uno::Any & rArg17,const uno::Any & rArg18,const uno::Any & rArg19,const uno::Any & rArg20,const uno::Any & rArg21,const uno::Any & rArg22,const uno::Any & rArg23,const uno::Any & rArg24,const uno::Any & rArg25,const uno::Any & rArg26,const uno::Any & rArg27,const uno::Any & rArg28,const uno::Any & rArg29,const uno::Any & rArg30)959 uno::Reference< excel::XRange > SAL_CALL ScVbaApplication::Intersect(
960         const uno::Reference< excel::XRange >& rArg1, const uno::Reference< excel::XRange >& rArg2,
961         const uno::Any& rArg3, const uno::Any& rArg4, const uno::Any& rArg5, const uno::Any& rArg6,
962         const uno::Any& rArg7, const uno::Any& rArg8, const uno::Any& rArg9, const uno::Any& rArg10,
963         const uno::Any& rArg11, const uno::Any& rArg12, const uno::Any& rArg13, const uno::Any& rArg14,
964         const uno::Any& rArg15, const uno::Any& rArg16, const uno::Any& rArg17, const uno::Any& rArg18,
965         const uno::Any& rArg19, const uno::Any& rArg20, const uno::Any& rArg21, const uno::Any& rArg22,
966         const uno::Any& rArg23, const uno::Any& rArg24, const uno::Any& rArg25, const uno::Any& rArg26,
967         const uno::Any& rArg27, const uno::Any& rArg28, const uno::Any& rArg29, const uno::Any& rArg30 )
968         throw (script::BasicErrorException, uno::RuntimeException)
969 {
970     if( !rArg1.is() || !rArg2.is() )
971         DebugHelper::exception( SbERR_BAD_PARAMETER, rtl::OUString() );
972 
973     // initialize the result list with 1st parameter, join its ranges together
974     ListOfScRange aList;
975     lclAddToListOfScRange( aList, uno::Any( rArg1 ) );
976 	lclJoinRanges( aList );
977 
978     // process all other parameters, this updates the list with intersection
979     lclIntersectRanges( aList, uno::Any( rArg2 ) );
980     lclIntersectRanges( aList, rArg3 );
981     lclIntersectRanges( aList, rArg4 );
982     lclIntersectRanges( aList, rArg5 );
983     lclIntersectRanges( aList, rArg6 );
984     lclIntersectRanges( aList, rArg7 );
985     lclIntersectRanges( aList, rArg8 );
986     lclIntersectRanges( aList, rArg9 );
987     lclIntersectRanges( aList, rArg10 );
988     lclIntersectRanges( aList, rArg11 );
989     lclIntersectRanges( aList, rArg12 );
990     lclIntersectRanges( aList, rArg13 );
991     lclIntersectRanges( aList, rArg14 );
992     lclIntersectRanges( aList, rArg15 );
993     lclIntersectRanges( aList, rArg16 );
994     lclIntersectRanges( aList, rArg17 );
995     lclIntersectRanges( aList, rArg18 );
996     lclIntersectRanges( aList, rArg19 );
997     lclIntersectRanges( aList, rArg20 );
998     lclIntersectRanges( aList, rArg21 );
999     lclIntersectRanges( aList, rArg22 );
1000     lclIntersectRanges( aList, rArg23 );
1001     lclIntersectRanges( aList, rArg24 );
1002     lclIntersectRanges( aList, rArg25 );
1003     lclIntersectRanges( aList, rArg26 );
1004     lclIntersectRanges( aList, rArg27 );
1005     lclIntersectRanges( aList, rArg28 );
1006     lclIntersectRanges( aList, rArg29 );
1007     lclIntersectRanges( aList, rArg30 );
1008 
1009     // create the VBA Range object
1010     return lclCreateVbaRange( mxContext, getCurrentDocument(), aList );
1011 }
1012 
Union(const uno::Reference<excel::XRange> & rArg1,const uno::Reference<excel::XRange> & rArg2,const uno::Any & rArg3,const uno::Any & rArg4,const uno::Any & rArg5,const uno::Any & rArg6,const uno::Any & rArg7,const uno::Any & rArg8,const uno::Any & rArg9,const uno::Any & rArg10,const uno::Any & rArg11,const uno::Any & rArg12,const uno::Any & rArg13,const uno::Any & rArg14,const uno::Any & rArg15,const uno::Any & rArg16,const uno::Any & rArg17,const uno::Any & rArg18,const uno::Any & rArg19,const uno::Any & rArg20,const uno::Any & rArg21,const uno::Any & rArg22,const uno::Any & rArg23,const uno::Any & rArg24,const uno::Any & rArg25,const uno::Any & rArg26,const uno::Any & rArg27,const uno::Any & rArg28,const uno::Any & rArg29,const uno::Any & rArg30)1013 uno::Reference< excel::XRange > SAL_CALL ScVbaApplication::Union(
1014         const uno::Reference< excel::XRange >& rArg1, const uno::Reference< excel::XRange >& rArg2,
1015         const uno::Any& rArg3, const uno::Any& rArg4, const uno::Any& rArg5, const uno::Any& rArg6,
1016         const uno::Any& rArg7, const uno::Any& rArg8, const uno::Any& rArg9, const uno::Any& rArg10,
1017         const uno::Any& rArg11, const uno::Any& rArg12, const uno::Any& rArg13, const uno::Any& rArg14,
1018         const uno::Any& rArg15, const uno::Any& rArg16, const uno::Any& rArg17, const uno::Any& rArg18,
1019         const uno::Any& rArg19, const uno::Any& rArg20, const uno::Any& rArg21, const uno::Any& rArg22,
1020         const uno::Any& rArg23, const uno::Any& rArg24, const uno::Any& rArg25, const uno::Any& rArg26,
1021         const uno::Any& rArg27, const uno::Any& rArg28, const uno::Any& rArg29, const uno::Any& rArg30 )
1022         throw (script::BasicErrorException, uno::RuntimeException)
1023 {
1024     if( !rArg1.is() || !rArg2.is() )
1025         DebugHelper::exception( SbERR_BAD_PARAMETER, rtl::OUString() );
1026 
1027     ListOfScRange aList;
1028     lclAddToListOfScRange( aList, uno::Any( rArg1 ) );
1029     lclAddToListOfScRange( aList, uno::Any( rArg2 ) );
1030     lclAddToListOfScRange( aList, rArg3 );
1031     lclAddToListOfScRange( aList, rArg4 );
1032     lclAddToListOfScRange( aList, rArg5 );
1033     lclAddToListOfScRange( aList, rArg6 );
1034     lclAddToListOfScRange( aList, rArg7 );
1035     lclAddToListOfScRange( aList, rArg8 );
1036     lclAddToListOfScRange( aList, rArg9 );
1037     lclAddToListOfScRange( aList, rArg10 );
1038     lclAddToListOfScRange( aList, rArg11 );
1039     lclAddToListOfScRange( aList, rArg12 );
1040     lclAddToListOfScRange( aList, rArg13 );
1041     lclAddToListOfScRange( aList, rArg14 );
1042     lclAddToListOfScRange( aList, rArg15 );
1043     lclAddToListOfScRange( aList, rArg16 );
1044     lclAddToListOfScRange( aList, rArg17 );
1045     lclAddToListOfScRange( aList, rArg18 );
1046     lclAddToListOfScRange( aList, rArg19 );
1047     lclAddToListOfScRange( aList, rArg20 );
1048     lclAddToListOfScRange( aList, rArg21 );
1049     lclAddToListOfScRange( aList, rArg22 );
1050     lclAddToListOfScRange( aList, rArg23 );
1051     lclAddToListOfScRange( aList, rArg24 );
1052     lclAddToListOfScRange( aList, rArg25 );
1053     lclAddToListOfScRange( aList, rArg26 );
1054     lclAddToListOfScRange( aList, rArg27 );
1055     lclAddToListOfScRange( aList, rArg28 );
1056     lclAddToListOfScRange( aList, rArg29 );
1057     lclAddToListOfScRange( aList, rArg30 );
1058 
1059     // simply join together all ranges as much as possible, strip out covered ranges etc.
1060 	lclJoinRanges( aList );
1061 
1062     // create the VBA Range object
1063     return lclCreateVbaRange( mxContext, getCurrentDocument(), aList );
1064 }
1065 
1066 void
Volatile(const uno::Any & aVolatile)1067 ScVbaApplication::Volatile( const uno::Any& aVolatile )  throw ( uno::RuntimeException )
1068 {
1069 	sal_Bool bVolatile = sal_True;
1070 	aVolatile >>= bVolatile;
1071 	return;
1072 }
1073 
1074 void SAL_CALL
DoEvents()1075 ScVbaApplication::DoEvents() throw ( uno::RuntimeException )
1076 {
1077 }
1078 ::sal_Bool SAL_CALL
getDisplayFormulaBar()1079 ScVbaApplication::getDisplayFormulaBar() throw ( css::uno::RuntimeException )
1080 {
1081 	sal_Bool bRes = sal_False;
1082 	ScTabViewShell* pViewShell = excel::getCurrentBestViewShell( mxContext );
1083 	if ( pViewShell )
1084 	{
1085 		SfxBoolItem sfxFormBar( FID_TOGGLEINPUTLINE);
1086 		SfxAllItemSet reqList(  SFX_APP()->GetPool() );
1087 		reqList.Put( sfxFormBar );
1088 
1089 		pViewShell->GetState( reqList );
1090 		const SfxPoolItem *pItem=0;
1091 		if ( reqList.GetItemState( FID_TOGGLEINPUTLINE, sal_False, &pItem ) == SFX_ITEM_SET )
1092 			bRes =   ((SfxBoolItem*)pItem)->GetValue();
1093 	}
1094 	return bRes;
1095 }
1096 
1097 void SAL_CALL
setDisplayFormulaBar(::sal_Bool _displayformulabar)1098 ScVbaApplication::setDisplayFormulaBar( ::sal_Bool _displayformulabar ) throw ( css::uno::RuntimeException )
1099 {
1100 	ScTabViewShell* pViewShell = excel::getCurrentBestViewShell( mxContext );
1101 	if ( pViewShell && ( _displayformulabar !=  getDisplayFormulaBar() ) )
1102 	{
1103 		SfxBoolItem sfxFormBar( FID_TOGGLEINPUTLINE, _displayformulabar);
1104 		SfxAllItemSet reqList(  SFX_APP()->GetPool() );
1105 		SfxRequest aReq( FID_TOGGLEINPUTLINE, 0, reqList );
1106 		pViewShell->Execute( aReq );
1107 	}
1108 }
1109 
1110 uno::Any SAL_CALL
Caller(const uno::Any &)1111 ScVbaApplication::Caller( const uno::Any& /*aIndex*/ ) throw ( uno::RuntimeException )
1112 {
1113 	StarBASIC* pBasic = SFX_APP()->GetBasic();
1114 	SbMethod* pMeth = (SbMethod*)pBasic->GetRtl()->Find( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FuncCaller") ), SbxCLASS_METHOD );
1115 	uno::Any aRet;
1116 	if ( pMeth )
1117 	{
1118 		SbxVariableRef refTemp = pMeth;
1119 		// forces a broadcast
1120 		SbxVariableRef pNew = new  SbxMethod( *((SbxMethod*)pMeth));
1121                 OSL_TRACE("pNew has type %d and string value %s", pNew->GetType(), rtl::OUStringToOString( pNew->GetString(), RTL_TEXTENCODING_UTF8 ).getStr() );
1122 		aRet = sbxToUnoValue( pNew );
1123 	}
1124 	return aRet;
1125 }
1126 
GetOpenFilename(const uno::Any & rFileFilter,const uno::Any & rFilterIndex,const uno::Any & rTitle,const uno::Any & rButtonText,const uno::Any & rMultiSelect)1127 uno::Any SAL_CALL ScVbaApplication::GetOpenFilename(
1128         const uno::Any& rFileFilter, const uno::Any& rFilterIndex, const uno::Any& rTitle,
1129         const uno::Any& rButtonText, const uno::Any& rMultiSelect ) throw (uno::RuntimeException)
1130 {
1131     uno::Sequence< uno::Any > aArgs( 6 );
1132     aArgs[ 0 ] <<= getThisExcelDoc( mxContext );
1133     aArgs[ 1 ] = rFileFilter;
1134     aArgs[ 2 ] = rFilterIndex;
1135     aArgs[ 3 ] = rTitle;
1136     aArgs[ 4 ] = rButtonText;
1137     aArgs[ 5 ] = rMultiSelect;
1138 	uno::Reference< lang::XMultiComponentFactory > xFactory( mxContext->getServiceManager(), uno::UNO_SET_THROW );
1139 	uno::Reference< XExecutableDialog > xFilePicker( xFactory->createInstanceWithArgumentsAndContext(
1140         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.OpenFilePicker" ) ), aArgs, mxContext ), uno::UNO_QUERY_THROW );
1141     return xFilePicker->execute();
1142 }
1143 
GetSaveAsFilename(const uno::Any & rInitialFileName,const uno::Any & rFileFilter,const uno::Any & rFilterIndex,const uno::Any & rTitle,const uno::Any & rButtonText)1144 uno::Any SAL_CALL ScVbaApplication::GetSaveAsFilename(
1145         const uno::Any& rInitialFileName, const uno::Any& rFileFilter, const uno::Any& rFilterIndex,
1146         const uno::Any& rTitle, const uno::Any& rButtonText ) throw (uno::RuntimeException)
1147 {
1148     uno::Sequence< uno::Any > aArgs( 6 );
1149     aArgs[ 0 ] <<= getThisExcelDoc( mxContext );
1150     aArgs[ 1 ] = rInitialFileName;
1151     aArgs[ 2 ] = rFileFilter;
1152     aArgs[ 3 ] = rFilterIndex;
1153     aArgs[ 4 ] = rTitle;
1154     aArgs[ 5 ] = rButtonText;
1155 	uno::Reference< lang::XMultiComponentFactory > xFactory( mxContext->getServiceManager(), uno::UNO_SET_THROW );
1156 	uno::Reference< XExecutableDialog > xFilePicker( xFactory->createInstanceWithArgumentsAndContext(
1157         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.SaveAsFilePicker" ) ), aArgs, mxContext ), uno::UNO_QUERY_THROW );
1158     return xFilePicker->execute();
1159 }
1160 
1161 uno::Reference< frame::XModel >
getCurrentDocument()1162 ScVbaApplication::getCurrentDocument() throw (css::uno::RuntimeException)
1163 {
1164     return getCurrentExcelDoc(mxContext);
1165 }
1166 
1167 rtl::OUString&
getServiceImplName()1168 ScVbaApplication::getServiceImplName()
1169 {
1170 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaApplication") );
1171 	return sImplName;
1172 }
1173 
1174 uno::Sequence< rtl::OUString >
getServiceNames()1175 ScVbaApplication::getServiceNames()
1176 {
1177 	static uno::Sequence< rtl::OUString > aServiceNames;
1178 	if ( aServiceNames.getLength() == 0 )
1179 	{
1180 		aServiceNames.realloc( 1 );
1181 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Application" ) );
1182 	}
1183 	return aServiceNames;
1184 }
1185 
1186 namespace application
1187 {
1188 namespace sdecl = comphelper::service_decl;
1189 sdecl::vba_service_class_<ScVbaApplication, sdecl::with_args<false> > serviceImpl;
1190 extern sdecl::ServiceDecl const serviceDecl(
1191     serviceImpl,
1192     "ScVbaApplication",
1193     "ooo.vba.excel.Application" );
1194 }
1195