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